Hello, I am really new to Corona sdk and I like it so far.
I’ve trying to make a stick figure to run left or right while touching the desire button and when no button is pressed the stick figure stands.
I kinda made it work, except for a little problem, when the stick figures stops running not always stays in the stand position
code:
[blockcode]
display.setStatusBar(display.HiddenStatusBar)
local color = display.newRect(0,0,display.contentWidth,display.contentHeight)
color:setFillColor(255,255,255)
local btnLeft = display.newRect(0,0,70,70)
btnLeft:setFillColor(150,0,0);
local btnRight = display.newRect(0,0,70,70)
btnRight:setFillColor(150,0,0);
local textLeft = display.newText(“Left”,18,22,native.systemFont, 20)
textLeft:setTextColor( 255,255,255 )
local textRight = display.newText(“Right”,12,22,native.systemFont, 20)
textRight:setTextColor( 255,255,255 )
local leftGroup = display.newGroup()
local rightGroup = display.newGroup()
leftGroup:insert(btnLeft)
leftGroup:insert(textLeft)
rightGroup:insert(btnRight)
rightGroup:insert(textRight)
leftGroup.x=70; leftGroup.y=230;
rightGroup.x=340; rightGroup.y=230;
local sprite = require(“sprite”)
local spriteSheet = sprite.newSpriteSheet(“runningStick.png”, 100, 100) – this sprite sheet has 10 pictures of the running stick figure and the picture 11 is the stick figure standing
local heroSet = sprite.newSpriteSet(spriteSheet, 1, 11)
sprite.add(heroSet, “running”, 1, 10, 450, 0) – 10 frames of the stick figure running
sprite.add(heroSet, “stop”, 11, 11, 1, 0) – this is just one frame of the stick figure in the stand position
local hero = sprite.newSprite(heroSet)
x = display.contentWidth/2
y = display.contentHeight/2
hero.x = x
hero.y = y
hero:prepare(“stop”)
function run()
if (hero.direction == “left”) then
hero.xScale = -1
hero.x = hero.x - 5
elseif (hero.direction == “right”) then
hero.xScale = 1
hero.x = hero.x + 5
end
end
function btnLeft:touch(e)
if (e.phase == “began”) then
hero:prepare(“running”)
hero:play()
display.getCurrentStage():setFocus( btnLeft )
hero.direction = “left”
Runtime:addEventListener(“enterFrame”,run)
elseif (e.phase == “ended”) then
hero:prepare(“stop”)
hero:play() – if I remove this, explained below
display.getCurrentStage():setFocus( nil )
Runtime:removeEventListener(“enterFrame”,run)
end
end
function btnRight:touch(e)
if (e.phase == “began”) then
hero:prepare(“running”)
hero:play()
display.getCurrentStage():setFocus( btnRight )
hero.direction = “right”
Runtime:addEventListener(“enterFrame”,run)
elseif (e.phase == “ended”) then
hero:prepare(“stop”)
hero:play() – if I remove this, explained below
display.getCurrentStage():setFocus( nil )
Runtime:removeEventListener(“enterFrame”,run)
end
end
btnLeft:addEventListener(“touch”,btnLeft)
btnRight:addEventListener(“touch”,btnRight)
[/blockcode]
Now if I remove the hero:play() and leave only the hero:prepare(“stop”), it actually works(since It stops any current animation and set the first frame of the new animation), but the standing stick figures is always blinking (dissappear and appear).
Hope you can help and sorry about my messy code but I’ve been trying stuff.
Thank you in advance. [import]uid: 128340 topic_id: 22287 reply_id: 322287[/import]