Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
[NewBie] How to stop the animation in event ENTER_FRAME? - Gideros Forum

[NewBie] How to stop the animation in event ENTER_FRAME?

lantb87lantb87 Member
edited January 2016 in Game & application design
Dear all,

I created in the event ENTER_FRAME that allow the image run automatic from Top to Bottom. But I want when I click on this image then I can move it . But unfortunately my code was not ran as I expected.

Below is my code :
leftAnimal = Core.class(Sprite)
 
function leftAnimal:init()
	frameList = {
		"images/animal/pig.png",
		"images/animal/cow.png",
		"images/animal/lamb.png",
		"images/animal/chicken.png"
	}
	-- create a Bitmap for each frame
	self.frames = {}
	for i = 1, #frameList do
		self.frames[i] = Bitmap.new(Texture.new(frameList[i]))
	end
 
	self.nframes = #frameList
 
	-- add first Bitmap as a child
	self.frame = math.random(4)
	self.frames[self.frame]:setAnchorPoint(0.5,0.5)
	self:addChild(self.frames[self.frame])
 
	-- subframe is used to adjust the speed of animation
	self.subframe = 0
 
	-- set initial position
	self:setPosition(conf.width/2 - conf.width/4,0)
	self:setScale(0.1,0.1)
	-- set the speed of the animal from top to bottom
	self.speedy = 2
 
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
	self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
	self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
end
 
function leftAnimal:onEnterFrame()
	-- get the current position
	local x,y = self:getPosition()
 
	-- change the position according to the speed
	y = y + self.speedy
 
	-- if the animal is out of the screen, set new speed and position
	if y > conf.height then
		self.subframe = self.subframe + 1
		self:removeChild(self.frames[self.frame])
		self.frame = math.random(4)
		self.frames[self.frame]:setAnchorPoint(0.5,0.5)
		self:addChild(self.frames[self.frame])
		self.subframe = 0
		self.speedy = 2
		x = conf.width/2 - conf.width/4
		y = 0
	end
	-- set the new position
	self:setPosition(x, y)
end
 
function leftAnimal:onMouseDown(event)
	if self:hitTestPoint(event.x, event.y) then
		self.focus = true
		print("onclick animal")
		self.x0 = event.x
		self.y0 = event.y
		event:stopPropagation()
	end
end
 
function leftAnimal:onMouseMove(event)
	print("on mouse move")
	if self.isFocus then
		print("self focus")
		local dx = event.x - self.x0
		local dy = event.y - self.y0
		self:setX(self:getX() + dx)
		self:setY(self:getY() + dy)
		self.x0 = event.x
		self.y0 = event.y
		event:stopPropagation()
	end
end

Comments

  • Please anyone can help me?
  • hgy29hgy29 Maintainer
    Accepted Answer
    Hi @lantb87,
    I think you got it nearly right, except for your self.focus variable management.
    First, you should have some piece of code that set back this variable to false (in a MOUSE_UP event probably), then your enter frame handler should also check that focus variable, and abort processing if it is true.

    At least this is how I understood your code and what you are trying to do :)

    Likes: lantb87

    +1 -1 (+1 / -0 ) Share on Facebook
  • lantb87lantb87 Member
    edited January 2016
    Hello @hgy29,

    Thank you alot for your comment, it save me!

    I had change my code and it working. here is my code :
    leftAnimal = Core.class(Sprite)
     
    function leftAnimal:init()
    	frameList = {
    		"images/animal/pig.png",
    		"images/animal/cow.png",
    		"images/animal/lamb.png",
    		"images/animal/chicken.png"
    	}
    	-- create a Bitmap for each frame
    	self.frames = {}
    	for i = 1, #frameList do
    		self.frames[i] = Bitmap.new(Texture.new(frameList[i]))
    	end
     
    	self.nframes = #frameList
     
    	-- add first Bitmap as a child
    	self.frame = math.random(4)
    	self.frames[self.frame]:setAnchorPoint(0.5,0.5)
    	self:addChild(self.frames[self.frame])
     
    	-- subframe is used to adjust the speed of animation
    	self.subframe = 0
     
    	-- set initial position
    	self:setPosition(conf.width/2 - conf.width/4,0)
    	self:setScale(0.1,0.1)
    	-- set the speed of the animal from top to bottom
    	self.speedy = 2
    	self.isFocus = false
    	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    	self:addEventListener(Event.MOUSE_DOWN, self.onMouseDown, self)
    	self:addEventListener(Event.MOUSE_MOVE, self.onMouseMove, self)
    	self:addEventListener(Event.MOUSE_UP, self.onMouseUp, self)
    end
     
    function leftAnimal:onEnterFrame()
    	-- get the current position
    	if self.isFocus == false then
    		local x,y = self:getPosition()
     
    		-- change the position according to the speed
    		y = y + self.speedy
     
    		-- if the animal is out of the screen, set new speed and position
    		if y > conf.height then
    			self.subframe = self.subframe + 1
    			self:removeChild(self.frames[self.frame])
    			self.frame = math.random(4)
    			self.frames[self.frame]:setAnchorPoint(0.5,0.5)
    			self:addChild(self.frames[self.frame])
    			self.subframe = 0
    			self.speedy = 2
    			x = conf.width/2 - conf.width/4
    			y = 0
    		end
    		-- set the new position
    		self:setPosition(x, y)
    	end
     
    end
     
    function leftAnimal:onMouseDown(event)
    	if self:hitTestPoint(event.x, event.y) then
    		self.isFocus = true
    		print("onclick animal")
    		self.x0 = event.x
    		self.y0 = event.y
    		event:stopPropagation()
    	end
    end
     
    function leftAnimal:onMouseMove(event)
    	print("on mouse move")
    	print(self.isFocus)
    	if self.isFocus then
    		local dx = event.x - self.x0
    		local dy = event.y - self.y0
    		self:setX(self:getX() + dx)
    		self:setY(self:getY() + dy)
    		self.x0 = event.x
    		self.y0 = event.y
    		event:stopPropagation()
    	end
    end
     
    function leftAnimal:onMouseUp(event)
    	if self.isFocus then
    		self.isFocus = false
    		event:stopPropagation()
    	end
    end
  • Hi
    change this condition:
    if self.isFocus == false then
    -- your code
    end
    to:
    if self.isFocus then return end
    -- your code
    (better style I guess.)
Sign In or Register to comment.