Howdy, Stranger!

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

-HELP- Help to Jump and Move Left and Right

edited February 2016 in Questions Posts: 1

I'm just new, and I'm trying to do a simple up and down, left and right sprite. I have these "arrows", which are just boxes. The thing is that I have a lot of variables, like "x=x+speed", but when that line is in the code, the UP button just moves the sprite left, vut when I add the text "y=y+speed" everything moves diagonally. Help please? Thanks :smile:

Code:


displayMode(FULLSCREEN) function setup() speed=0 direction=100 x=WIDTH/2 y=HEIGHT/2 rx1=450 rx2=550 rx3=500 ry1=50 ry2=130 end function draw() background(40, 40, 50) fill(255) text("touch the right or left side of the screen",WIDTH/2,HEIGHT-50) sprite("Planet Cute:Heart",x,y,direction,150) rect(450,50,70,70) rect(550,50,70,70) rect(500,130,70,70) x=x+speed y=y+speed if x>WIDTH+50 then x=-50 end if x<-50then x=WIDTH+50 end end function touched(t) if t.state == BEGAN and t.x>400 and t.x<500 and t.y>30 and t.y <130 then speed=-3 direction=-100 else if t.state == ENDED then speed=0 direction=-100 end if t.state == BEGAN and t.x>500 and t.x<600 and t.y>30 and t.y<130 then speed=3 direction=100 else if t.state == ENDED then speed=0 direction=100 end end if t.state == BEGAN and t.x>450 and t.x<550 and t.y>80 and t.y<180 then speed=3 direction=100 else if t.state == ENDED then speed=0 direction=100 end end end end

Comments

  • dave1707dave1707 Mod
    Posts: 7,522

    @andresac You need 2 speeds, one for the x direction and one for the y direction. The up button should set the plus y speed and 0 x speed. The left button a minus x speed and a 0 y speed. The right button a plus x speed and a 0 y speed

  • dave1707dave1707 Mod
    Posts: 7,522

    @andresac Do a forum search on jump. There a several examples of jumping.

  • Posts: 688

    Here's one

    Obviously forgot to add the appropriate values in the y direction.

  • dave1707dave1707 Mod
    Posts: 7,522

    @andresac Here's your code modified a little with a simple jump routine.

    displayMode(FULLSCREEN)
    
    function setup()
        xspeed=0
        yspeed=0
        x=WIDTH/2
        y=HEIGHT/2
        yjump=0
    end
    
    function draw()
        background(40, 40, 50)
        fill(255)
        text("touch the right or left side of the screen",WIDTH/2,HEIGHT-50)
        sprite("Planet Cute:Heart",x,y)  
        rect(450,50,70,70)
        rect(550,50,70,70)
        rect(500,130,70,70)
        x=x+xspeed
        y=y+yspeed
        if x>WIDTH+50 then
            x=-50
        end
        if x<-50then
            x=WIDTH+50
        end
        if jump and yjump<150 then
            yjump=yjump+yspeed
        elseif yjump>0 then
            jump=false
            yspeed=-3
            yjump=yjump+yspeed
        else
            yspeed=0
        end
    end
    
    function touched(t)
        if t.state == BEGAN then
            if t.x>450 and t.x<520 and t.y>50 and t.y <120 then
                xspeed=-3
            end
            if t.x>550 and t.x<620 and t.y>50 and t.y<120 then
                xspeed=3
            end
            if t.x>500 and t.x<570 and t.y>130 and t.y<200 then
                jump=true
                yspeed=3
            end
        end
        if t.state == ENDED then
            xspeed=0
        end
    end
    
Sign In or Register to comment.