It looks like you're new here. If you want to get involved, click one of these buttons!
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
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
Thank you alot for your comment, it save me!
I had change my code and it working. here is my code :
change this condition:
if self.isFocus == false then
-- your code
end
to:
if self.isFocus then return end
-- your code
(better style I guess.)