I get stuck on a "Jump Function".

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
haxx_blaster
Posts: 13
Joined: Fri Jan 20, 2006 4:21 pm

I get stuck on a "Jump Function".

Post by haxx_blaster »

I get stuck on a "Jump Function", i have been trying to get it to work all day, i get freaking nuts..

I simply want my character to jump up and down on a button release.
Please help me out guys, so i can continue with my game.

And if it turn out good i may be release it in public.


My code looked something like this.

Code: Select all

if pad:cross() then
time = time + 1
yP = yP - time
if time == 70 then
time = 0
yP = yP + time
if time = 70 then
time = 0
end
end
end
CaptainChickenpants
Posts: 5
Joined: Wed Feb 01, 2006 2:29 am

Post by CaptainChickenpants »

Hi,
I am not really a lua programmer, just found out about the PSP luaplayer.

Looking at your code there are a few points.

a) You are using ==, and = interchangeably. Not sure on the lua syntax but if it is similar to C then your second comparison with time,is actually an assignment.
b) I have to assume that you are initialising yP to something useful somewhere?
c) The second comparison with time will never occur (even with it corrected to be a comparison rather than an assinment) as you are clearing time to zero.
d) Indent your code and mistakes like this are easier to see.

try something more like

Code: Select all

height=70
time =0
.
.
if pad:cross() then
    time=time+1
    if (time>=height) then
        yP=2*height-time
    else
        yP = time
    end
    if (time> (2*height)) then
       yP=0
    end
end
To give the bounce a bit more 'bounce; try setting the height using a sine wave function.

CC
haxx_blaster
Posts: 13
Joined: Fri Jan 20, 2006 4:21 pm

Post by haxx_blaster »

I just wrote what i remember of my code, i haden't my PSP plugged in at the time.
I did not get any errors, but i will try your method, thanks. :)
haxx_blaster
Posts: 13
Joined: Fri Jan 20, 2006 4:21 pm

Post by haxx_blaster »

It did not work.
What happened on the screen was kind of like this.

Code: Select all

if pad:cross() then
yP == 0
end
Thanks anyway.
CaptainChickenpants
Posts: 5
Joined: Wed Feb 01, 2006 2:29 am

Post by CaptainChickenpants »

Ok. I have modified the code in the tutorial with the smiley so that it does a bounce when you press the up key.

Code: Select all

System.usbDiskModeActivate()
green = Color.new(0, 255, 0)
time = 0
ytime= 0
height=70


pi = math.atan(1) * 4
background = Image.load("background.png")
smiley = Image.load("smiley.png")
while true do
	pad = Controls.read()
	
	if pad:start() then
		break
	end
	screen:blit(0, 0, background, 0, 0, background:width(), background:height(), false)
	x = math.sin(pi * 2 / 250 * time) * 200 + 220.5
	y = 172 
	if pad:up() then
		ytime=ytime+1
	elseif ytime>0 then
		ytime=ytime+1
	end	


      if ytime>height then
          yP=2*height-ytime
      else
          yP = ytime
      end
      if (ytime> (2*height)) then
         ytime=0
	   yP = ytime
      end

	screen:blit(x, y-yP, smiley)
	time = time + 1
	if time >= 500 then
		time = 0
	end
 
	screen.waitVblankStart()
	screen.flip()
 end
As I mentioned before, you will get a more realistic looking bounce if you use a sine wave to calculate the height. Look at the code in the tutorial to see that works.

CC
haxx_blaster
Posts: 13
Joined: Fri Jan 20, 2006 4:21 pm

Post by haxx_blaster »

I got it to work in my code, but the picture is at yP = 0 from the beginning, so thats strange..

Could you tell me how stuff like "yP=2*height-ytime" works?
CaptainChickenpants
Posts: 5
Joined: Wed Feb 01, 2006 2:29 am

Post by CaptainChickenpants »

Well you are wanting the object to go up and then down. So the total distance travelled is height *2.
Height is the halfway point, so once you are past height, you want to find out how far you are away from 2*height.
The easiest way to understand these things is to draw it on a bit of paper and plot it at various stages. e.g. at yTime = 0, yTime = height/2, yTime = height, yTime = 3*Height/2 and yTime = 2*height.


When you are happy with that then have a look at using a sine wave. The problem with your current scheme is that your sprite will start moving up at a constant speed, stop and drop back at the same speed. Using a sine wave would allow your character to slow down as it reached the top.

CC
Post Reply