Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
creating a bounce on click - Gideros Forum

creating a bounce on click

simransimran Member
edited June 2014 in Relax cafe
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

  • OZAppsOZApps Guru
    edited June 2014
    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
    You are either increasing or decreasing the y by 20 if the screen is touched and then set the position to +20, which is in essence the same so you will hardly get any movement.

    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.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    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
  • simransimran Member
    edited June 2014
    No, no. initial position of y is 0. so here is what I changed the code to :
    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
  • OZAppsOZApps Guru
    Accepted Answer
    @Simran,
    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
    if self.touchStarted then
    speed = -16
    self.touchStarted = false
    end
    y = y + speed;
    speed = speed + 2
    self.myAnim:setPosition(x,y)
    end
    you need to learn how to dry run code to see what happens, you start with if there is a touch started, you set the speed to -16 and then immediately set the touch started to false, so the next time around the speed is -16 again as touchStarted is false.

    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.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    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
Sign In or Register to comment.