I’ve literally been messing with this for over 2 hours and can’t figure out how to make this work properly. I’m trying to make it so that when the user touches the leftButton, rightButton, or jumpButton it will change to the appropriate sprite(s). I’ve literally managed to make it work every way except for how I want it to. For example, my player stayed in the “standing” at one point while I was moving him, then immediately upon release he started animating… I just don’t understand.
I’ll post my code after I cleaned out the mess I made in the movement functions.
function new()
local localGroup = display.newGroup()
--Enable multitouch
system.activate("multitouch")
--Enable physics
local physics = require( "physics" )
physics.start()
local sprite = require("sprite")
--Draw Objects Here
--
--Player---------------------------------------------------------
local playerSpriteSheet = sprite.newSpriteSheet("images/player-sprites.png", 32, 32)
local playerSet = sprite.newSpriteSet(playerSpriteSheet, 1, 4)
--Sprites [set, "name", fromthisimg, tothisimg, animationtime, how many times will it perform?]
sprite.add(playerSet, "standing", 1, 1, 1, 1)
sprite.add(playerSet, "running", 1, 3, 450, 0)
sprite.add(playerSet, "jumping", 4, 4, 1, 1)
--The Player-----------------------------------------------------
local player = sprite.newSprite(playerSet)
player:setReferencePoint(display.CenterReferencePoint)
player.x = \_W/2
player.y = \_H - 135
physics.addBody( player, { density=1, friction=1, bounce=0 } )
player.isFixedRotation = true
jumping = false
moving = false
player:prepare("standing")
player:play()
--The Level-------------------------------------------------------
local ground = display.newImageRect("images/ground.png", 1024, 24)
ground:setReferencePoint(display.CenterReferencePoint)
ground.x = \_W/2
ground.y = \_H - 120
physics.addBody( ground, "static", { density=1, friction=1, bounce=0 } )
--
--
--Movement Buttons-------------------------------------------------
local leftButton = display.newImageRect("images/leftButton.png", 85, 85)
leftButton:setReferencePoint(display.CenterReferencePoint)
leftButton.x = \_W / 10
leftButton.y = \_H - 45
local rightButton = display.newImageRect("images/rightButton.png", 85, 85)
rightButton:setReferencePoint(display.CenterReferencePoint)
rightButton.x = \_W / 4
rightButton.y = \_H - 45
local jumpButton = display.newImageRect("images/jumpButton.png", 85, 85)
jumpButton:setReferencePoint(display.CenterReferencePoint)
jumpButton.x = \_W - 75
jumpButton.y = \_H - 45
-----Functions-----------------------------------------
-------------------------------------------------------
-------------------------------------------------------
----
-----Function to change scene
function changeScene(e)
if(e.phase == "ended") then
director:changeScene(e.target.scene)
end
end
--- Movement Functions
function moveLeft(event)
player.xScale = -1
player.x = player.x - 3
end
function moveRight(event)
player.xScale = 1
player.x = player.x + 3
end
----Movement
function leftButton:touch(event)
if (event.phase == "began") then
Runtime:addEventListener("enterFrame", moveLeft)
display.getCurrentStage():setFocus(event.target, event.id);
elseif (event.phase == "ended") then
Runtime:removeEventListener("enterFrame", moveLeft)
display.getCurrentStage():setFocus(event.target, nil);
end
end
function rightButton:touch(event)
if (event.phase == "began") then
Runtime:addEventListener("enterFrame", moveRight)
display.getCurrentStage():setFocus(event.target, event.id);
elseif (event.phase == "ended") then
Runtime:removeEventListener("enterFrame", moveRight)
display.getCurrentStage():setFocus(event.target, nil);
end
end
--Jumping
function jumpButton:touch(event)
if (event.phase == "began" and jumping == false) then
player:applyForce(0, -1500, player.x, player.y)
display.getCurrentStage():setFocus(event.target, event.id);
jumping = true
else
display.getCurrentStage():setFocus(event.target, nil);
end
end
--Check if the player can jump
function jumpCheck(event)
if(event.phase == "began") then
jumping = false
end
end
-----Inserting Objects to localGroup
localGroup:insert(player)
localGroup:insert(leftButton)
localGroup:insert(rightButton)
localGroup:insert(jumpButton)
-----Event Listeners
leftButton:addEventListener("touch", leftButton)
rightButton:addEventListener("touch", rightButton)
jumpButton:addEventListener("touch", jumpButton)
player:addEventListener("collision", jumpCheck)
return localGroup
end
[import]uid: 131400 topic_id: 23272 reply_id: 323272[/import]
