Hello,
I want my character to bounce much like flappy bird and for that here is what I did but instead of bouncing, the character just keeps flickering it position and does not even move.Where am I doing it wrong? I do not want to use box2d for this since all I want is the flappy jump.Any help on that, please?
function Play: whenTouched()
self.touchStarted= true
print("up")
end
function Play: whenuntouched()
self.touchStarted = false
print("down")
end
function Play: whenEntersFrame()
x, y = self.myAnim:getPosition()
if self.touchStarted then
x = x
y = y-20
else
x=x
y=y+20
end
self.myAnim:setPosition(x, y+20)
end
Comments
if the y position of the anim was say 100
y = y - 20
100 - 20 = 80
then, setPosition(x, y+20)
80 + 20 = 100
#2, if you do not want to move the x, then you can also use the getY and setY functions instead
#3, to solve your problem #1, either decrease a value of more than 20 or set the position to a value smaller than 20. However remember that both will have different results one will make the bird fall faster even if you are tapping the screen to make it flap and move up.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
Note: Please let me know which keys I need to use to paste code properly, could not figure out .
function Play: whenTouched()
self.touchStarted= true
x,y = self.myAnim:getPosition()
if self.touchStarted then
speed = -16
self.touchStarted = false
end
y = y + speed;
speed = speed + 2
self.myAnim:setPosition(x,y)
end
The problem is it's not even close to making a jump like flappy bird.it goes up on touch, but does not fall like gravity is pulling it. Infact, after a couple of seconds, it looks like the bird is at its initial position as well as the jump position(two birds on screen), please help with this.
Infact, if screen is not touched, it on its own keep going up and down instead of simply falling.
Thanks
with the snippets that you provide and the problem you present, it is difficult to formulate a solution.
To know how to move a character on screen using enterFrame and moving it up/down on touch, you can find plenty of samples on the net (if you search) some might be in languages other than Lua.
If you want to see one probable solution, then you can have a look at page 354 of Learn Lua for iOS Game Development where I have discussed how to move the helicopter and also included movement with an accelerometer. You can adapt that to work with touch.
I am not sure of your code, but if you were to increase the Y value of an object it would provide the illusion of falling down and if you were to decrease it, it would move upwards.
Lastly, your code
You are adding 2 to the speed to increase it (as -16 would make the sprite drop by 16 pixels) but it will always be -16 the next time around.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps