Howdy, Stranger!

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

In this Discussion

Can't get physics working

edited July 12 in Questions Posts: 4

I'm trying to get a physics game but when I'm applyForce then it's going below the floor

Boy = class()

function Boy:init()
    -- you can accept and set parameters here
    self.bodysprite = "Small World:Bush"
    self.body = physics.body(CIRCLE,50)
    self.body.x = WIDTH/2
    self.body.y = HEIGHT/2
end

function Boy:draw()
    sprite(self.bodysprite,self.body.x,self.body.y,50,50)
    -- Codea does not automatically call this method
end

function Boy:touched(touch)
    -- Codea does not automatically call this method
    if CurrentTouch.x > WIDTH/2 then
        self.body:applyForce(vec2(800,0))
    end
end
Tagged:

Comments

  • dave1707dave1707 Mod
    edited July 12 Posts: 6,102

    @Purple107 Not sure about all you're trying to do, but I took your class and made some changes. Move your finger on the left side of the screen to move the bush to the right. Move your finger on the right side to move it left. Also, any finger movement moves it up. See how to use the touched function correctly instead of using CurrentTouch.

    EDIT: Added EDGE to create a floor.

    displayMode(FULLSCREEN)
    
    function setup()
        e=physics.body(EDGE,vec2(0,0),vec2(WIDTH,0))
        b=Boy()
    end
    
    function draw()
        background(40, 40, 50)
        b:draw()
    end
    
    function touched(t)
        b:touched(t)
    end
    
    Boy = class()
    
    function Boy:init()
        self.bodysprite = "Small World:Bush"
        self.body = physics.body(CIRCLE,50)
        self.body.x = WIDTH/2
        self.body.y = HEIGHT/2
    end
    
    function Boy:draw()
        sprite(self.bodysprite,self.body.x,self.body.y,50,50)
    end
    
    function Boy:touched(touch)
        if touch.state==MOVING then
            if touch.x < WIDTH/2 then
                self.body:applyForce(vec2(100,150))
            else
                self.body:applyForce(vec2(-100,150))
            end
        end
    end
    
  • Posts: 4

    @dave1707 thank you again for the code your awesome.
    :)

Sign In or Register to comment.