Setting the linear velocity of a physics object from an external module in Corona SDK

StackOverflow https://stackoverflow.com/questions/22844378

  •  27-06-2023
  •  | 
  •  

Domanda

I am working on a classic RPG and need some help with joystick based movement. The player should be able to move the character in any direction with a circular pad that he moves with his thumb. This should be familiar from FPS games where a pad controls the left/right and forward/backward movement of the character. My code for constructing the character is this:

    local player = {}
    local player_mt = { __index = player }

    function player.new( --[[ parameters for player instance ]]-- )
      local newPlayer = { --[[ player attributes here ]]-- }

      newPlayer.image = display.newImage("testImage.png")

      physics.addBody( newPlayer.image, "kinematic" )

      return setmetatable( newPlayer, player_mt )
    end

And this is the function for moving the character (still in the same .lua file):

    function player:startMoving( xMotion, yMotion)
      self:setLinearVelocity( xMotion*player.speedModifier, yMotion*player.speedModifier)
    end

Now in my main.lua file where I am testing out all of my functions, I initialize a new player, create a circle for the joystick, add a touch event listener to it, and use that to call my move function:

    local physics = require("physics")
    physics.start()

    local player = require("player")

    local myPlayer = player.new( --[[player params]]-- )

    myControl = display.newCircle( 300, 300, 100 )
    myControl:setFillColor( 100, 100, 100 )
    myControl:addEventListener( "touch", controlCharacter )

    function controlCharacter( event )
      if( event.phase == "began" ) then
        x = myControl.x
        y = myControl.y
      elseif( event.phase == "moved" ) then
        myControl.x = event.x
        myControl.y = event.y
        myPlayer:startMoving( myControl.x - event.x, myControl.y - event.y )
      elseif( event.phase == "ended" ) then
        myControl.x = x
        myControl.y = y
      end
    end

When I run this, I get the following error: attempt to call method 'setLinearVelocity' (a nil value). It's like it doesn't recognize the player as a physics object, but if I change the player physics object type in the player.lua file to dynamic,

    physics.addBody( newPlayer.image, "kinematic" )  ___
                                                        |
    physics.addBody( newPlayer.image, "dynamic" )    <--

then the gravity function operates fine, it falls right off the screen. Can anyone help me with what's going on here?

È stato utile?

Soluzione

OK, I got the answer to my question. When I initialized the player's image as a physics object, I initialized it as newPlayer.image, so when I set the linear velocity, I needed to put

    self.image:setLinearVelocity 

instead of

    self:setLinearVelocity

since self.image, not self, is the physics object.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top