| View previous topic :: View next topic |
| Author |
Message |
haxx_blaster
Joined: 20 Jan 2006 Posts: 13
|
Posted: Mon Jan 30, 2006 6:43 pm Post subject: I get stuck on a "Jump Function". |
|
|
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: | 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 |
|
|
| Back to top |
|
 |
CaptainChickenpants
Joined: 01 Feb 2006 Posts: 5
|
Posted: Wed Feb 01, 2006 2:42 am Post subject: |
|
|
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: |
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 |
|
| Back to top |
|
 |
haxx_blaster
Joined: 20 Jan 2006 Posts: 13
|
Posted: Wed Feb 01, 2006 6:39 am Post subject: |
|
|
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. :) |
|
| Back to top |
|
 |
haxx_blaster
Joined: 20 Jan 2006 Posts: 13
|
Posted: Wed Feb 01, 2006 3:48 pm Post subject: |
|
|
It did not work.
What happened on the screen was kind of like this.
| Code: | if pad:cross() then
yP == 0
end |
Thanks anyway. |
|
| Back to top |
|
 |
CaptainChickenpants
Joined: 01 Feb 2006 Posts: 5
|
Posted: Wed Feb 01, 2006 8:20 pm Post subject: |
|
|
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: |
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 |
|
| Back to top |
|
 |
haxx_blaster
Joined: 20 Jan 2006 Posts: 13
|
Posted: Thu Feb 02, 2006 4:24 am Post subject: |
|
|
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? |
|
| Back to top |
|
 |
CaptainChickenpants
Joined: 01 Feb 2006 Posts: 5
|
Posted: Thu Feb 02, 2006 7:07 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
|