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 in Questions 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)
    --make your charactar
    ellipse(WIDTH/4,HEIGHT/4.2,100)
    --make the ground
    fill(70, 255, 0, 255)
    rect(WIDTH/4, HEIGHT/50,550, 170)
    rectMode(RADIUS)
end

Comments

  • dave1707dave1707 Mod
    Posts: 7,468

    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.

  • dave1707dave1707 Mod
    Posts: 7,468

    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
    
  • Thanks

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

  • dave1707dave1707 Mod
    Posts: 7,468

    Try using Gravity.x or Gravity.y .

  • dave1707dave1707 Mod
    Posts: 7,468

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

        x=x+Gravity.x*10  
    
  • @dave1707
    Thanks a lot for helping me in my learning so far :)

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

  • IgnatzIgnatz Mod
    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:

    My Codea ebook may also help you, it is 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 :'(

  • IgnatzIgnatz Mod
    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: 808

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

  • IgnatzIgnatz Mod
    Posts: 5,396

    @TokOut - read the reference, you don't need to ask dave for the answer.

  • @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?

  • dave1707dave1707 Mod
    Posts: 7,468

    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.

  • IgnatzIgnatz Mod
    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.

  • 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)
            text("Mad Dash",WIDTH/2, HEIGHT/1.19)
            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
    
  • dave1707dave1707 Mod
    edited January 2016 Posts: 7,468

    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.

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

  • Still it isn't working it shows a black screen

  • dave1707dave1707 Mod
    Posts: 7,468

    @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)
            text("Mad Dash",WIDTH/2, HEIGHT/1.19)
            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
    
  • 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)
            text("Mad Dash",WIDTH/2, HEIGHT/1.19)
            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[1]--ground=spike
            tempr=box[1]--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
    

    `

  • dave1707dave1707 Mod
    edited January 2016 Posts: 7,468

    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.

  • dave1707dave1707 Mod
    Posts: 7,468

    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

  • @dave1707
    It worked thanks a lot that saved a lot of my problems :)

  • IgnatzIgnatz Mod
    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.

  • dave1707dave1707 Mod
    edited January 2016 Posts: 7,468

    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.

  • @Ignatz
    Thanks for the trick for debuging

Sign In or Register to comment.