Hi,
I have a sprite sheet (code below) and I’m trying to get it to move left, right, and jump, when a button is pressed? Any help would be great!
–Running Sprite
local sheetData = { width=100, height=70, numFrames=6, sheetContentWidth=200, sheetContentHeight=210 }
local mySheet = graphics.newImageSheet( “runningcat2small.png”, sheetData )
local sequenceData = {
{ name = “normalRun”, start=1, count=6, time=800 },
{ name = “fastRun”, frames={ 1,2,4,5, }, time=250 }
}
local animation = display.newSprite( mySheet, sequenceData )
animation.x = 50 --the sprite position horizontally
animation.y = 215–the sprite position vertically
animation:play()
– Add left joystick button
local left = display.newImage (“images/btn_arrow.png”)
left.x = 45; left.y = 280;
left.rotation = 180;
– Add right joystick button
local right = display.newImage (“images/btn_arrow.png”)
right.x = 120; right.y = 282;
– Add Jump button
local up = display.newImage (“images/btn_arrow.png”)
up.x = 440; up.y = 280;
up.rotation = 270;
– 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 )
– Move character
local function moveguy (event)
guy.x = guy.x + motionx;
end
Runtime:addEventListener(“enterFrame”, moveguy)
– When left arrow is touched, move character left
function left:touch()
motionx = -speed;
end
left:addEventListener(“touch”,left)
– When right arrow is touched, move character right
function right:touch()
motionx = speed;
end
right:addEventListener(“touch”,right)
– Make character jump
function up:touch(event)
if(event.phase == “began” and playerInAir == false) then
playerInAir = true
guy:setLinearVelocity( 0, 700 )
end
end
up:addEventListener(“touch”,up)