Hello,
I want to access a function removeBlock() of an instance of block.lua indexed in my array b[1] from main.lua. *I don't want to remove b[1] i want to removechild the instance at the index in b array.* The long story is I want to load my instances in an array and destroy them in loops in main, like I could in as3.
--main.lua
require("block")
main = Core.class(Sprite)
a = {}
b= {}
Yprevious=1;
start=0
updown=0
tick=0
-- load background image and add it to the stage
local background = Bitmap.new(Texture.new("field.png"))
stage:addChild(background)
-- create ball sprites
local ball1 = Ball.new("ball1.png")
-- create building blocks sprites
local block1 = Block.new("block.png")
-- and add ball sprites to the stage
stage:addChild(ball1)
stage:addChild(block1)
-- add block to stage
for i=0,4 do
b[1]=block1
end
a[1]=ball1;
function onEnterFrame(event)
for i=1,1
do
local x, y = a[i]:getPosition();
x = x+1;
if (start==0) or (updown==1) then
y=y-1;
start=1;
end
if updown==0 then
y=y+1
end
if (tick>=100) and (updown==1) then
updown=0;tick=0
end
if (tick>=100) and (updown==0) then
end
if x > 350 then
x= 1;
end
a[i]:setPosition(x, y);
Yprevious=y;
end
tick=tick+1;
end
function mouseD(event)
updown=1;
d=collCheck_Box2B ( a[1], b[1])
if d==true then
print("collide");
b[1]
--!!!!! what do I add here to call the RemoveBlock function of the instance of block.lua at b[1]
end
end
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage:addEventListener(Event.MOUSE_DOWN, mouseD);
--MikeHart's code from codesnippets
function collCheck_Box2B ( source, target )
local rw = source:getWidth()/2.0 + target:getWidth()/2.0
local rh = source:getHeight()/2.0 + target:getHeight()/2.0
if math.abs(source:getY() - target:getY()) < rh then
if math.abs(source:getX() - target:getX() ) < rw then
return true
end
end
return false
end |
--block.lua
Block = Core.class(Sprite)
function Block:init(texture)
local bitmap = Bitmap.new(Texture.new(texture))
self:addChild(bitmap)
self:setX(math.random(0, 270))
self:setY(math.random(0, 430))
end
function RemoveBlock()
print("remove")
--How do I remove this instance
end |
Comments
if you want to remove block from stage then, definition of function:
But why did you add the Block: before the function, Why don't I need the require? I've read stack overflow questions about modules and require, etc and it was this simple. Can you explain this, and where did you learn lua?
PSS. This code is a modified version of jumpballs. I'm bending it into a blitz bomber game. What you don't get is probably my attempts to bend lua to my as3 codebase. I'm used to having an entity framework for all my projects but lua really throws me. Thanks again!
In your case Block is a classname, you can define class properties
Block.someproperty = 1
and methods function Block:somemethod() end
Of course you can define method with dot:
I'm a concept learner too(microbiology) but in programming I'm a language learner.
Just leftover junk from a previous iteration. I rarely start from scratch.