I have a sprite that is moving right and starts turning/rotating when on MOUSE_DOWN event. ENTER_FRAME then keeps turning and rotating sprite until we lift finger (MOUSE_UP). The sprite should travel in a direction it is facing. I have no box2d,just normal sprites (movieclip) and bitmaps.
two questions:
1. Is this method ok (wait for MOUSE_DOWN to set rotation flag, then rotate on ENTER_FRAME and set a "stop" flag for sprite to stop rotating on MOUSE_UP ) for when we want to *do something as long as someone keeps their finger pressed on screen" . It works but I am not sure if this is ok method.
2. The problem I have is that when the speed of sprite is slow then when I press the screen the sprite starts rotating BUT it still moves in the same direction until it reaches certain angle. Any idea why is that? If the speed is faster then the rotation and direction seems to be much more synchronized. I am guessing it is something with math (cos,sin etc) calculations? So it looks like when speed is high that the sprite changes direction in small angles (eg 1,2,3,4,5,..) so rotation and direction appear synchronized. But when sprite moves slow then it changes direction in "jumps" (eg only when it reaches angles like 5,10,15,...) but rotation remains smooth so it looks ugly - sprite has slightly rotated but still keeps moving in previous direction.
I am using standard rotation calculation that was recommended on this forum and is found on many forums on rotating/sprites following direction.
Here is the relevant code, modified so it wont be too much to read.
in Player:init() I call self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
Below code is onenterframe.
self.turn is 0 in the beginning and on MOUSE_UP event so sprite stops turning. On MOUSE_DOWN we call Player:move() so self.angle will increase by 1 on every frame. Self.speed is just how fast sprite is moving.
Any idea why sprite rotation AND direction appear synchronized when the speed is high (eg 5 and up) but not when speed is low (1).
function Player:onEnterFrame(event)
if (self.angle > 360) then self.angle = 0 end
self.angle = self.angle + self.turn
local cx, cy = self:getX() ,self:getY() -- current x, y pos
local xo,yo -- x, y offset
self.speed = self.speed or 1 -- movement speed / distance to move
local Radtheta = self.angle * math.pi/180
xo = math.cos(Radtheta) * self.speed
yo = math.sin(Radtheta) * self.speed
cx = cx + xo
cy = cy + yo
self:setRotation(self.angle)
self:setPosition(cx,cy)
end
function Player:move()
self.turn = 1 --this is a flag
end
function Player:stay()
self.turn = 0
self.mc1:gotoAndPlay(1)
end |
Comments
http://members.giderosmobile.com/knowledgebase.php?action=displayarticle&id=41
1) the method is completely ok and probably the best one there is
2) will need to play with it and will be right back
Looks a lot like Roomba
http://www.screenr.com/embed/X2dN
Second, it now seems that the problem is somewhere completely different than I though. Calculations are ok, the problem is somewhere with sprite or maybe setting anchorpoint of sprite (I am using your gideromadeeasy lua file).
Please replace your init code with this one (I have 3 PNGs but I replaced it with one ball.png so no need for 3 pics):
any idea why this might be? I would like my main character be a sprite ,not just bitmap, so I can do other manipulations, like animation etc.
one more time thank you.
I finally figured it out: it is GiderosCodingEasy.lua that is causing the problem! Try adding GiderosCodingEasy.lua (just adding is enough!) to your example and you will get the same problem. This is so strange - is GiderosCodingEasy.lua overwritting soem function or something? Remove the file from the project and everything starts working in your example.
I will test it a little more tommorow because I am not sure if it is only GiderosCodingEasy.lua or also something with sprites.
Without GiderosCodingEasy I cant set the sprite anchor though
its all due to rounding for some reason.
Basically if in GiderosCodingEasy you find the method:
I will have to reexamine the purpose and error of the roundings later
So I tried to continue making this game and the moment I moved the position of the sprite it didn't rotate around itself anymore but around x-100 or something like that for some reason - anchor point seems to stay behind the sprite:
I tried your solution here :
http://giderosmobile.com/forum/discussion/3888/universal-spritesetanchorpoint0-5-0-5/p1
but it produces the same result (maybe i'm doing it wrong?). I loaded 3 bitmaps then set this (the rest remains the same):
any ideas? This also completely messes up TNT collision detection when checking collision with another sprite but I'm gonna leave this out for now,maybe this will automatically solve if I can solve this problem first. Thanks for any help.
However then I made a new project with only 1 file and it worked! I tried it again with giderosmadeeasy and setting anchorpoint to movieclip/sprite it also worked.
So this means the problem is elsewhere.I have cut down the unnecessary code and the only problem I can think of lies in game.lua file/sprite that hold the player sprite. But why? I did not set any anchor or anything in the game file. Here is my game file:
So if I have only player sprite in main.lua and run it then everything works. But when I put player in player.lua and then make game.lua and call player from there then this problem happens. In short, sprite game holds sprite player and when I set position of player then the anchorpoint doesnt move with player.
I have read some forum posts here about child sprites and anchor points but I don't really understand it how this applies to my case. Should I set rotate my Game sprite? But that would make no sense, game is just a holder for all sprites.
BTW, is setting bitmap anchorpoints better solution than using giderosmadeeasy and setting sprite or movieclip anchorpoints?
the problem was that I set the position of movieclip (which is wrong)
I have an idea of more elegant, efficient and easy way of anchor point implementation, but unfortunately not enough time to try, if it has any consequences or road blocks. Hopefully will dedicated an evening to try it out