Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
call function that removes an instance indexed in an array in main — Gideros Forum

call function that removes an instance indexed in an array in main

oldschoololdschool Member
edited March 2012 in General questions
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

  • ar2rsawseenar2rsawseen Maintainer
    Honestly, I don't understand a lot of stuff you do there, but
    if you want to remove block from stage then, definition of function:
    function Block:RemoveBlock()
    print("remove")
    --remove from parent
    self:removeFromParent()
    end
    And then call it like this:
    b[1]:RemoveBlock()
  • Worked Perfectly thank you very much!

    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!
  • ar2rsawseenar2rsawseen Maintainer
    I don't under stand, why for example, you put block instance inside same array element 5 times? :)
    for i=0,4 do
    b[1]=block1
    end
    But ok, so by default all files you attach to Gideros project are executed automatically, thus you don't need to call require. Gideros has it's own implementation of Objects as classes, not like default modules in lua.

    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:
    Block.somemethod = function() 
    --do something 
    end
    but it means that self variable will not reference class, but it's own context, meaning:
    function Block:init()
    self.someproperty = "somevalue"
    end
     
    function Block:methodOne()
    -- you can access someproperty, because it is the same context
    --self references to same instance of class Block
    print(self.someproperty) -- will print out somevalue
    end
     
    --assigning function to property, call it static method
    Block.methodTwo = function()
    --this function has it's own context and is not related to class Block
    print(self.someproperty) -- will print out nil
    end
    --it's the same as simply defining function
    --completely unrelated to class Block
    local function methodTwo()
    end
    I've simply learned so much different languages, starting from "low-level" up to weakly typed ones, that I almost don't need to learn new languages anymore, can grasp everything from examples. The power of concept learning, not language learning. :)
  • Thank you for the explanation. I'm glad your here, have a great day!

    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.
    for i=0,4 do
    b[1]=block1
    end
Sign In or Register to comment.