Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Player Movement not working when the sprite is in a table
Started by MOULTC15A Jun 09 2019 08:25 PM

- - - - -
2 replies to this topic

Best Answer davebollinger , 10 June 2019 - 07:05 AM

inside movePlayer() the playerCostume variable is probably not set to the correct one - it's probably still set to the last one (#2) you created in playerLook().  you're using a lot of global variables, and not showing where they're declared, or if they're modified anywhere else, but based on the code you show...  if your "costume" variable is kept current, then i'd modify movePlayer() to either this:

local function movePlayer(event)
  playerCostume = player[costume] -- added
  playerCostume.x = playerCostume.x + motionx -- same
end

or this:

 

local function movePlayer(event)
  player[costume].x = player[costume].x + motionx -- modified
end

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

MOULTC15A

[GLOBAL: userInfoPane.html]
MOULTC15A
  • Observer

  • 3 posts
  • Corona SDK

Hey this is my first time making a game in corona for a school project. I have a working code for player movement so when the left or right arrow is pressed, the player on the screen will move left or right. However, i put my sprite in a table (as there are two different costumes for the sprite) and now the movement will not work. Here is my code:

 

 

-- Function for the player table
local function playerLook()
 for i=1,2 do
  playerCostume = display.newImageRect( mainGroup, objectSheet, i, 30, 50 )
  table.insert( player, playerCostume )
  physics.addBody( playerCostume, { radius=30, isSensor=true } )
  playerCostume.x = display.contentCenterX
  playerCostume.y = display.contentHeight - 95
  playerCostume.myName = "costume"
  playerCostume.isVisible = false
 end
 costume = 1
 player[costume].isVisible = true
end
 
 
--When left arrow is touched, move character left
local function leftTouch()
 motionx = -speed;
end
left:addEventListener("touch", leftTouch)
 
 
--When right arrow is touched, move character right
local function rightTouch()
 motionx = speed;
end
right:addEventListener("touch", rightTouch)
 
 
--Move character
local function movePlayer( event )
 playerCostume.x = playerCostume.x + motionx;
end
Runtime:addEventListener( "enterFrame", movePlayer)
 
 
--Stop character movement when no arrow is pushed
local function stop( event )
 if event.phase == "ended" then
  motionx = 0;
 end
end
Runtime:addEventListener("touch", stop )

 



[TOPIC: post.html]
#2

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,373 posts
  • Corona SDK

  Best Answer

inside movePlayer() the playerCostume variable is probably not set to the correct one - it's probably still set to the last one (#2) you created in playerLook().  you're using a lot of global variables, and not showing where they're declared, or if they're modified anywhere else, but based on the code you show...  if your "costume" variable is kept current, then i'd modify movePlayer() to either this:

local function movePlayer(event)
  playerCostume = player[costume] -- added
  playerCostume.x = playerCostume.x + motionx -- same
end

or this:

 

local function movePlayer(event)
  player[costume].x = player[costume].x + motionx -- modified
end


[TOPIC: post.html]
#3

MOULTC15A

[GLOBAL: userInfoPane.html]
MOULTC15A
  • Observer

  • 3 posts
  • Corona SDK

Thanks, the player movement works fine now  :)




[topic_controls]
[/topic_controls]