#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# magicskillz's questions

edited December 2015 Posts: 87

First question: How do you make your charactar jump?

How do you make you charectar jump when you touch the screen?
Some easy example code that you can use as a layout:

``````function setup()
end
-- This function gets called once every frame
function draw ()
-- This sets a dark background color
background(40, 40, 50)
fill(255, 2, 0, 255)
ellipse(WIDTH/4,HEIGHT/4.2,100)
--make the ground
fill(70, 255, 0, 255)
rect(WIDTH/4, HEIGHT/50,550, 170)
end``````
Tagged:

• Posts: 8,445

First, you should add the function touched(t). When the screen is touched, and the touch state is equal to BEGAN, then you add an amount to the y value of the character. After it jumps to whatever height you want,, you subtract an amount until the character is back to the original position.

• Posts: 8,445

Here is a simple example.

``````function setup()
v=0
x=300
y=300
end

function draw()
background(40, 40, 50)
stroke(255)
strokeWidth(3)
line(0,250,WIDTH,250)
sprite("Platformer Art:Guy Standing",x,y)
y=y+v
if y>400 then
v=-2
elseif y<=300 then
v=0
end
end

function touched(t)
if t.state==BEGAN then
v=2
end
end
``````
• Posts: 87

Thanks

• Posts: 87

Also how do you make it so when you tilt your device your charectar moves

• Posts: 8,445

Try using Gravity.x or Gravity.y .

• Posts: 8,445

Add this line to the end of the draw function in my code above.

``````    x=x+Gravity.x*10
``````
• Posts: 87

@dave1707
Thanks a lot for helping me in my learning so far • Posts: 87

How do you make the camera follow the charactar in your game?

• Posts: 5,396

translate(char.x,char.y)

I'd like to suggest you do some reading about these things, because while we are happy to solve problems and provide general guidance, we don't have the time to provide personal tuition on every aspect of Codea.

So if you are trying to make a game, and planning to ask us how to do each and every step in it, because you know very little about Codea, I'd like you to stop right now - and first learn as much as you can about Codea (and Lua, the language behind it).

There is lots of helpful stuff on the wiki link above, and I have also written a lot of stuff on Codea, which you can find here:

The next step, if you have a question, is to google the forum to see if there is an answer (search box at upper right). It's very important that you figure out how to solve as many problems as possible, by yourself. That's an important part of being a programmer.

Only then, if you are still stuck, should you ask us a question. That way, you don't waste our time.

And that's important to you, too, because if people keep asking us unnecessary questions, we stop answering them.

• edited December 2015 Posts: 87

@Ignatz
sigh. You have a point. I won't ask questions on Codea • edited December 2015 Posts: 5,396

You CAN ask questions, but do your homework first. The forum should be last on your list of places to look for solutions, not the first.

• Posts: 889

@dave1707, isn't gravity a tag for the iPad's Gravity position?

• Posts: 5,396

• Posts: 87

@Ignatz
I DID do my research and I cant find a answer maybe I'm not typing in the right words to the search box. But how do allow your character to move in the air while jumping. My guess is that you have to add the x to the y but how?

• Posts: 8,445

If the Sprite is moving in the x direction when the character jumps, it will still be moving in the x direction. Or do you mean that the character is stationary and when the character jumps, it moves in the x direction. If that's the case, then just add 1 or subtract 1 from the x value until the character stops the jump.

• edited January 2016 Posts: 5,396

@magicskillz - you won't find a specific answer to that question on the forum. But that's not where you should be looking.

To answer your question, you need to understand how to move stuff around on the screen, rather than how to move the character while jumping - because moving while jumping is simply moving around on the screen!

Moving stuff on the screen is covered in many tutorials, including my ebooks, and in lots of sample code. That's where you'll find the answer to questions like this. And if you don't know how to change the x and y values, you need to do some more reading about Codea before trying to program it.

• Posts: 87

Thx and by the way I do know how to change the x and y values but it didn't work

• Posts: 82
@magicskillz I strongly suggest that you read the ebook by Ignatz before asking these questions. The things you're asking concern rather basic things, and you won't make much progress if you never take time to study the matter a little bit.

It can be found here: https://www.dropbox.com/sh/mr2yzp07vffskxt/AABlplSGGFBTu8FzQy94lteua/Lua for beginners.pdf?dl=0
It helped me a lot when I started using Codea!
• edited January 2016 Posts: 87

Been looking at Ignatz's books and made this code but it's not working I also FIXED the code because of @dave1707's comment

``````supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
gameState=0
x=100
y=300
cam = vec2(0,0)
done=true
cb={x=60,y=300}
val=2
end
function draw()
if GameState==0 --mainscreen
then
font("Copperplate-Bold")
fontSize(75)
--title
fill(0, 255, 8, 255)
fontSize(25)
--Name
fill(235, 5, 10, 255)
textAlign(CORNER)
text("By Magic Skillz", WIDTH/6, HEIGHT/30)
if gameState==2 then
cam.x = cam.x - DeltaTime*119.3
background(40, 40, 50)
fill(255)
translate(cam.x,cam.y)
x=x+3
cb.x=cb.x+val -- move sprite
sprite("Planet Cute:Character Boy",cb.x,cb.y)
end
function jumpTween()
t1=tween(.5,cb,{y=400},tween.easing.cubicOut)
t2=tween(.5,cb,{y=300},tween.easing.cubicIn,tweenDone)
tween.sequence(t1,t2)
end

function duckTween()
t1=tween(.5,cb,{y=200},tween.easing.cubicOut)
t2=tween(.5,cb,{y=300},tween.easing.cubicIn,tweenDone)
tween.sequence(t1,t2)
end

function draw()
-- draw sprite
end

function tweenDone()
done=true   -- tween is done, allow next jump
end

function touched(t)
if t.state==MOVING and done then
done=false
if t.deltaY<0 then
duckTween()
end
end
if t.deltaY>0 then  -- flick up
jumpTween()
end
end
end
end
``````
• edited January 2016 Posts: 8,445

When you post code, put 3~'s on a line before and after you code. Check the upper and lower case letters on gamestate. You also have multiple setup() and draw() functions. You should only have one of each.

• Posts: 87

@dave1707
were do I have to fix the gamestate's?

• Posts: 87

Still it isn't working it shows a black screen

• Posts: 8,445

@magicskillz Try this.

``````supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
gameState=0
x=100
y=300
cam = vec2(0,0)
done=true
cb={x=60,y=300}
val=2
end

function draw()
if gameState==0 then
font("Copperplate-Bold")
fontSize(75)
--title
fill(0, 255, 8, 255)
fontSize(25)
text("Tap screen to start.",WIDTH/2, HEIGHT/2)
--Name
fill(235, 5, 10, 255)
textAlign(CORNER)
text("By Magic Skillz", WIDTH/6, HEIGHT/30)
end
if gameState==2 then
cam.x = cam.x - DeltaTime*119.3
background(40, 40, 50)
fill(255)
translate(cam.x,cam.y)
x=x+3
cb.x=cb.x+val -- move sprite
sprite("Planet Cute:Character Boy",cb.x,cb.y)
end
end

function jumpTween()
t1=tween(.5,cb,{y=400},tween.easing.cubicOut)
t2=tween(.5,cb,{y=300},tween.easing.cubicIn,tweenDone)
tween.sequence(t1,t2)
end

function duckTween()
t1=tween(.5,cb,{y=200},tween.easing.cubicOut)
t2=tween(.5,cb,{y=300},tween.easing.cubicIn,tweenDone)
tween.sequence(t1,t2)
end

function tweenDone()
done=true   -- tween is done, allow next jump
end

function touched(t)
if t.state==BEGAN and gameState==0 then
gameState=2
return
end
if t.state==MOVING and done then
done=false
if t.deltaY<0 then
duckTween()
else
jumpTween()
end
end
end
``````
• Posts: 87

Wow, thank you

• edited January 2016 Posts: 87

What is wrong with this code it says as an error:
Main:45: 'for' limit must be a
number
stack traceback:
Main:45: In function draw

I can't figure out were the problem is. Here is my code:
`

``````supportedOrientations(LANDSCAPE_ANY)
displayMode(FULLSCREEN)

function setup()
gameState=0
x=100
y=300
cam = vec2(0,0)
done=true
cb={x=60,y=300}
val=2
end
function draw()
if gameState==0 then
font("Copperplate-Bold")
fontSize(75)
--title
fill(0, 255, 8, 255)
fontSize(25)
text("Tap screen to start.",WIDTH/2, HEIGHT/2)
--Name
fill(235, 5, 10, 255)
textAlign(CORNER)
text("By Magic Skillz", WIDTH/6, HEIGHT/30)
end
if gameState==2 then
xpos=0
cam.x = cam.x - DeltaTime*119.3
background(40, 40, 50)
fill(255)
translate(cam.x,cam.y)
x=x+3
cb.x=cb.x+val -- move sprite
sprite("Planet Cute:Character Boy",cb.x,cb.y)
spike={1,0,0,0,0,0,0,0,0}
box={0,0,1,2,3,4,3,2,1,0}
for i = 1,12 do
--the value in the current position (i) of the ground array is the number of blocks high to
--draw at the current position
for h=1,spike[i] do
--draw the sprite at the current x position i * width of one sprite with the xpos
--offset which creates the movement
--the y position is calculated as a multiplier of the the height
sprite("Planet Cute:Ramp South",xpos+100*i-100,-40+70*h,100,100)
end
--repeat the process for the ceiling
for h=1,box[i] do
sprite("Planet Cute:Ramp South",xpos+100*i-100,-40+70*h,100,100)
end
end
temp=spike--ground=spike
tempr=box--roof=box
for c=1,#spike-1 do
spike[c]=spike[c+1]
box[c]=box[c+1]
end
--{b,c,d,d} becomes {b,c,d,a}
spike[#spike]=temp
box[#box]=tempr
end
end
function jumpTween()
t1=tween(.5,cb,{y=400},tween.easing.cubicOut)
t2=tween(.5,cb,{y=300},tween.easing.cubicIn,tweenDone)
tween.sequence(t1,t2)
end

function duckTween()
t1=tween(.5,cb,{y=200},tween.easing.cubicOut)
t2=tween(.5,cb,{y=300},tween.easing.cubicIn,tweenDone)
tween.sequence(t1)
end

function tweenDone()
done=true   -- tween is done, allow next jump
end

function touched(t)
if t.state==BEGAN and gameState==0 then
gameState=2
return
end
if t.state==MOVING and done then
done=false
if t.deltaY<0 then
duckTween()
else
jumpTween()
end
end
end
``````

`

• edited January 2016 Posts: 8,445

The problem is in or near line 45. One of the numbers in the `for` statement isn't a number. Also, just put 3~'s on a line by themselves. You also have a '. Edit your code above and correct the lines with the 3~'s.

• Posts: 8,445

The table spike has 9 values in it. The loop for i=1,12 causes i to go from 1 to 12. So the next loop for h=1,spike[i] causes spike[i] to go beyond the table size resulting in a nil value.

• edited January 2016 Posts: 87

@dave1707
Ok I'll try that

• Posts: 87

@dave1707
It worked thanks a lot that saved a lot of my problems • Posts: 5,396

@magicskillz - you need to be able to debug very simple errors like this yourself, if you want to tackle more complex projects in the future.

What I would do to find this error is, once I know the error is in the numbers for the `for` loop, is put in a print statement just before it, so I'd have this

``````    for i = 1,12 do
print (i, spike[i]) --<<<
for h=1,spike[i] do
``````

This would quickly pick up the problem.

• edited January 2016 Posts: 8,445

That's exactly what I did to find the error. When i became 10, spike[i] printed nil. I looked at the spike table and saw that it was only 9 entries long.

• Posts: 87

@Ignatz
Thanks for the trick for debuging