Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to create an instance with its own ENTER_FRAME event — Gideros Forum

How to create an instance with its own ENTER_FRAME event

zehrekenzehreken Member
edited November 2012 in General questions
Hi,
I want my object instances have their own ENTER_FRAME events but I couldn't manage to do it. Here is my test code.

Object.lua
Object = Core.class(Sprite)
local tmp = 0
local function onEnterFrame()
	tmp = tmp + 1
	print(tmp)
end
function Object:init()
	self:addEventListener(Event.ENTER_FRAME, onEnterFrame)
end
Main.lua
for i=1,4 do
	local object = Object.new()
end
I expect an output like this
0
0
0
0
1
1
1
1...
And this is what I get
0
1
2...
Only the first instance gives this output.

Comments

  • Dont forget to add third argument to :addEventListener
    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    have fun with our games~
    http://www.nightspade.com
  • Nope, it doesn't make a difference.
  • ScouserScouser Guru
    edited November 2012 Accepted Answer
    Try this
    Object.lua:
    Object = Core.class(Sprite)
    local function onEnterFrame(this)
    	this.tmp = this.tmp + 1
    	print(this.tmp)
    end
     
    function Object:init()
    	self:addEventListener(Event.ENTER_FRAME, onEnterFrame, self)
    	self.tmp = 0
    end
    main.lua:
    local instances = {}
     
    for i=1,4 do
    	instances[#instances+1] = Object:new()
    end
  • MellsMells Guru
    edited November 2012
    this works too :
     
    Object = Core.class(Sprite)
    function Object:init()
            self.tmp = 0
    	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    end
    function Object:onEnterFrame()
    	self.tmp = self.tmp + 1
    	print(self.tmp)
    end
    Maybe it would be more appropriate for some other cases than the Enter_frame event though.
    I like the fact that onEnterFrame() is not accessible from "outside" in Scouser's example.
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
Sign In or Register to comment.