Howdy, Stranger!

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

Help with physics bodies

edited August 2013 in Questions Posts: 59

For some reason, this code isn't working. It's updating the player velocity, but it only changes the x by 2 then sets it back to 374. Also, the physics body is constantly moving up, and its not event redrawing the character sprite at the right place. To add to that, I can't even set the physics body x and y in the setup, it just sets it to 374, 374. Please help!

function setup()
parameter.watch(PlayerY)
displayMode(FULLSCREEN_NO_BUTTONS)
supportedOrientations(LANDSCAPE_ANY)
floor1 = physics.body(EDGE,vec2(400,HEIGHT/2),vec2(600,HEIGHT/2))
Player = physics.body(CIRCLE,50)
physics.gravity(0,300)
parameter.watch("PlayerX")
parameter.watch("PlayerY")
floor1.linearVelocity = vec2(-200,0)
Scene = 1
scrollTitleScreen = 0
xVel = 0
yVel = 0
end

function draw()

--Draws the appropritate stagese
--Scenes:
--1 = Title Screen
--2 = Gamemode Select
--3 = Stage select
--4 = Ingame
scene = Scene
PlayerX = Player.x
PlayerY = Player.y
textMode(CENTER)
Player.restution = nil
floor1.restution = nil

Player.x = WIDTH/2
Player.y = HEIGHT/2
floor1.x = 500

if Scene == 1 then
strokeWidth(5)
background(0, 255, 251, 255)
font("Copperplate-Bold")
fill(0, 0, 0, 255)
fontSize(197)
text("VORTEX",WIDTH/2,HEIGHT -78)
rectMode(CENTER)
fill(0, 255, 251, 255)
stroke(0, 0, 0, 255)
rect(WIDTH/2,HEIGHT/2,250,126)
fill(0, 0, 0, 255)
fontSize(60)
text("PLAY",WIDTH/2,HEIGHT/2)

    elseif Scene == 2 then
        strokeWidth(5)
        fontSize(84)
        stroke(0, 0, 0, 255)
        fill(51,51,51,255)
    background(0,255,251,255)
    rectMode(CENTER)

    fill(0, 0, 0, 255)
    text("CHOOSE A GAMEMODE",WIDTH/2,HEIGHT -78)
    fill(0, 255, 251, 255)
    rect(WIDTH/2,HEIGHT/2,250,126) 
    fontSize(27)
    fill(0,0,0,250)
    text("DEATHMATCH",WIDTH/2,HEIGHT/2)
    rectMode(CORNER)
    fill(0, 255, 251, 255)
    rect(-3,-3,100,50)
    rectMode(CENTER)
    textMode(CORNER)
    fontSize(25)
    fill(0, 0, 0, 255)
    text("BACK",9,10)
    elseif Scene == 3 then
        background(0,255,251,255)
       strokeWidth(5)
        fontSize(84)
        stroke(0, 0, 0, 255)
    background(0,255,251,255)
    rectMode(CENTER)
    fill(0, 0, 0, 255)
    text("CHOOSE A STAGE",WIDTH/2,HEIGHT -78)
    fill(0, 255, 251, 255)
    rect(WIDTH/2,HEIGHT/2,250,126) 
    fontSize(49)
    fill(0,0,0,250)
    text("METRO",WIDTH/2,HEIGHT/2)
    rectMode(CORNER)
    fill(0, 255, 251, 255)
    rect(-3,-3,100,50)
    rectMode(CENTER)
    textMode(CORNER)
    fontSize(25)
    fill(0, 0, 0, 255)
    text("BACK",9,10)
    elseif Scene == 4 then
        rectMode(CENTER)
        stroke(0, 0, 0, 255)
        fill(127, 127, 127, 255)


                  strokeWidth(5)
        fontSize(84)
        stroke(0, 0, 0, 255)
    background(35, 47, 47, 255)
    rectMode(CENTER)
    fill(0, 0, 0, 255)
     rect(floor1.x,290,1200,90)
    fontSize(49)
    fill(0,0,0,250)
    rectMode(CORNER)
    fill(0, 255, 251, 255)
    rect(-3,-3,100,50)
    rectMode(CENTER)
    textMode(CORNER)
    fontSize(25)
    fill(0, 0, 0, 255)
    text("LEFT",9,10) 
            rectMode(CORNER)
    fill(0, 255, 251, 255)
    stroke(0, 0, 0, 255)
    rect(97,-3,100,50)
    rectMode(CENTER)
    textMode(CORNER)
    fontSize(25)
    fill(51, 51, 51, 255)
    text("RIGHT",109,10) 
    rectMode(CENTER)

    sprite("Planet Cute:Character Pink Girl",Player.x,Player.y)

end

end

--Buttons
--DeathMatch button
function touched(touch)
if touch.state == ENDED and touch.x > WIDTH/2 - 125 and touch.x < WIDTH /2 + 125 and touch.y < HEIGHT/2+63 and touch.y > HEIGHT/2- 63 and Scene == 2 then

    Scene = 3
    sound(SOUND_BLIT, 16900)

end
--Play button
if touch.state == ENDED and touch.x > WIDTH/2 - 125 and touch.x < WIDTH /2 + 125 and touch.y < HEIGHT/2+63 and touch.y > HEIGHT/2- 63 and Scene == 1 then

    Scene = 2
    sound(SOUND_BLIT, 16900)
    end

--Metro stage button
if touch.state == ENDED and touch.x > WIDTH/2 - 125 and touch.x < WIDTH /2 + 125 and touch.y < HEIGHT/2+63 and touch.y > HEIGHT/2- 63 and Scene == 3 then

    Scene = 4
    sound(SOUND_BLIT, 16900)
    floor1.x = 500
    floor1.y = 100
    end

--Back button (title screen)
if touch.state == ENDED and touch.x > 0 and touch.x < 100 and touch.y < 50 and touch.y > 0 and Scene ~= 1 and Scene ~= 4 then

    Scene = Scene -1
    sound(SOUND_BLIT, 16900)
    end  

if Scene == 4 then
if touch.x > WIDTH/2 then
    if touch.state == BEGAN or touch.state == MOVING then
        xVel = 100
        sound(SOUND_JUMP, 932)
        updatePlayerXVel()
        end
        elseif touch.state == ENDED then
            xVel = 0
            updatePlayerXVel()
        end
if touch.x < WIDTH/2 then
    if touch.state == BEGAN or touch.state == MOVING then
        xVel = - 100
        sound(SOUND_POWERUP, 28170)
        updatePlayerXVel()
        end
        elseif touch.state == ENDED then
            xVel = 0
            updatePlayerXVel()
        end

end

end
function updatePlayerXVel()
Player.linearVelocity = vec2(xVel)
end

Tagged:

Comments

  • edited August 2013 Posts: 355

    about putting all code in alignment so they do not show a distinct part to another. :)

    Or use pastebin.com

  • dave1707dave1707 Mod
    Posts: 8,192

    Not sure what this is supposed to do, but I modified some of the code. See comments that I added to certain lines. I think I commented all the lines I modified.


    function setup()      parameter.watch(PlayerY)      displayMode(FULLSCREEN_NO_BUTTONS)     supportedOrientations(LANDSCAPE_ANY)      floor1 = physics.body(EDGE,vec2(400,HEIGHT/2),vec2(600,HEIGHT/2))      Player = physics.body(CIRCLE,50)          -- moved the following line from draw     Player.x = WIDTH/2 Player.y = HEIGHT/2 floor1.x = 500          physics.gravity(0,30)     -- changed     parameter.watch("PlayerX")      parameter.watch("PlayerY")      floor1.linearVelocity = vec2(-200,0)      Scene = 1      scrollTitleScreen = 0      xVel = 0      yVel = 0  end function draw()     --Draws the appropritate stagese     --Scenes:     --1 = Title Screen     --2 = Gamemode Select     --3 = Stage select     --4 = Ingame     scene = Scene     PlayerX = Player.x     PlayerY = Player.y     textMode(CENTER)     Player.restution = nil     floor1.restution = nil          -- moved this line to setup     --Player.x = WIDTH/2 Player.y = HEIGHT/2 floor1.x = 500          if Scene == 1 then          strokeWidth(5)          background(0, 255, 251, 255)          font("Copperplate-Bold")          fill(0, 0, 0, 255)          fontSize(197)          text("VORTEX",WIDTH/2,HEIGHT -78)          rectMode(CENTER)          fill(0, 255, 251, 255)          stroke(0, 0, 0, 255)          rect(WIDTH/2,HEIGHT/2,250,126)          fill(0, 0, 0, 255)          fontSize(60) text("PLAY",WIDTH/2,HEIGHT/2)     elseif Scene == 2 then         strokeWidth(5)         fontSize(84)         stroke(0, 0, 0, 255)         fill(51,51,51,255)         background(0,255,251,255)         rectMode(CENTER)             fill(0, 0, 0, 255)         text("CHOOSE A GAMEMODE",WIDTH/2,HEIGHT -78)         fill(0, 255, 251, 255)         rect(WIDTH/2,HEIGHT/2,250,126)          fontSize(27)         fill(0,0,0,250)         text("DEATHMATCH",WIDTH/2,HEIGHT/2)         rectMode(CORNER)         fill(0, 255, 251, 255)         rect(-3,-3,100,50)         rectMode(CENTER)         textMode(CORNER)         fontSize(25)         fill(0, 0, 0, 255)         text("BACK",9,10)    elseif Scene == 3 then         background(0,255,251,255)         strokeWidth(5)         fontSize(84)         stroke(0, 0, 0, 255)         background(0,255,251,255)         rectMode(CENTER)         fill(0, 0, 0, 255)         text("CHOOSE A STAGE",WIDTH/2,HEIGHT -78)         fill(0, 255, 251, 255)         rect(WIDTH/2,HEIGHT/2,250,126)          fontSize(49)         fill(0,0,0,250)         text("METRO",WIDTH/2,HEIGHT/2)         rectMode(CORNER)         fill(0, 255, 251, 255)         rect(-3,-3,100,50)         rectMode(CENTER)         textMode(CORNER)         fontSize(25)         fill(0, 0, 0, 255)         text("BACK",9,10)    elseif Scene == 4 then         rectMode(CENTER)         stroke(0, 0, 0, 255)         fill(127, 127, 127, 255)         strokeWidth(5)         fontSize(84)         stroke(0, 0, 0, 255)         background(35, 47, 47, 255)         rectMode(CENTER)         fill(0, 0, 0, 255)          rect(floor1.x,290,1200,90)         fontSize(49)         fill(0,0,0,250)         rectMode(CORNER)         fill(0, 255, 251, 255)         rect(-3,-3,100,50)         rectMode(CENTER)         textMode(CORNER)         fontSize(25)         fill(0, 0, 0, 255)         text("LEFT",9,10)          rectMode(CORNER)         fill(0, 255, 251, 255)         stroke(0, 0, 0, 255)         rect(97,-3,100,50)         rectMode(CENTER)         textMode(CORNER)         fontSize(25)         fill(51, 51, 51, 255)         text("RIGHT",109,10)          rectMode(CENTER)         sprite("Planet Cute:Character Pink Girl",Player.x,Player.y)     end end --Buttons --DeathMatch button  function touched(touch)      if touch.state == ENDED and touch.x > WIDTH/2 - 125 and              touch.x < WIDTH /2 + 125 and touch.y < HEIGHT/2+63 and              touch.y > HEIGHT/2- 63 and Scene == 2 then         Scene = 3         sound(SOUND_BLIT, 16900)     end      --Play button      if touch.state == ENDED and touch.x > WIDTH/2 - 125 and              touch.x < WIDTH /2 + 125 and touch.y < HEIGHT/2+63 and              touch.y > HEIGHT/2- 63 and Scene == 1 then         Scene = 2         sound(SOUND_BLIT, 16900)     end     --Metro stage button      if touch.state == ENDED and touch.x > WIDTH/2 - 125 and          touch.x < WIDTH /2 + 125 and touch.y < HEIGHT/2+63 and              touch.y > HEIGHT/2- 63 and Scene == 3 then         Scene = 4         sound(SOUND_BLIT, 16900)         floor1.x = 500         floor1.y = 100     end     --Back button (title screen)      if touch.state == ENDED and touch.x > 0 and touch.x < 100 and          touch.y < 50 and touch.y > 0 and Scene ~= 1 and Scene ~= 4 then         Scene = Scene -1         sound(SOUND_BLIT, 16900)     end       if Scene == 4 then         if touch.x > WIDTH/2 then             if touch.state == BEGAN or touch.state == MOVING then                 xVel = 100                 sound(SOUND_JUMP, 932)                 updatePlayerXVel()             end         elseif touch.state == ENDED then             xVel = 0             updatePlayerXVel()         end         if touch.x < WIDTH/2 then             if touch.state == BEGAN or touch.state == MOVING then                 xVel = -100                 sound(SOUND_POWERUP, 28170)                 updatePlayerXVel()             end         elseif touch.state == ENDED then             xVel = 0             updatePlayerXVel()         end     end end  function updatePlayerXVel()      Player.linearVelocity = vec2(xVel)  end
  • Thanks! It works now.

Sign In or Register to comment.