new to corona, trouble with sprites

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]

Have you seen the sprite tutorial on Techority? It actually shows this exact kind of thing.

The blinking, etc. I suspect may have to do with your actual sheet.

If you wanted to upload a sample I could take a look - although I understand you may not want to do that, which is fine too.

Look at the tutorial I mentioned and let me know if that helps. Looking over the sheet and spacing, etc, would be good too.

Peach :slight_smile: [import]uid: 52491 topic_id: 22287 reply_id: 88838[/import]

Hello Peach,

I looked at your code from Techority, very nice by the way but when you use pause() it pauses the person at any frame, what I’m trying to do is if it stops moving to show the last frame of the sprite only.

Here is my sprite sheet, hope you can help me =)

Thank you in advance,
G

http://alturl.com/u3ogj [import]uid: 128340 topic_id: 22287 reply_id: 88953[/import]

That’s actually easy to fix, the pause thing :slight_smile:

You’d pause then on the next line do;

sprite.currentFrame = 10

(Or whatever number the last frame is.)

Let me know if that sorts out the problem - will look at the spritesheet if not :slight_smile: [import]uid: 52491 topic_id: 22287 reply_id: 88993[/import]

I really thought that would work, but since the set that is being animated doesnt have the last frame (standing position)currentFrame = 11 doesnt work of course.

local heroSet = sprite.newSpriteSet(spriteSheet, 1, 11)

sprite.add(heroSet, “running”, 1, 10, 450, 0)
sprite.add(heroSet, “stop”, 11, 11, 1, 0)

local hero = sprite.newSprite(heroSet)

So I thought if I:

hero:prepare(“stop”)
hero:pause()
hero.currentFrame = 11

But it doesnt work either, also I’m not sure why the prepare(“stop”) doesnt work in first place.

Btw, thank you for taking the time to answer me! :slight_smile:
G [import]uid: 128340 topic_id: 22287 reply_id: 89208[/import]

Try this code on its own;

[lua]display.setStatusBar( display.HiddenStatusBar)

require “sprite”

local bg = display.newRect( 0, 0, 480, 320 )

local spriteSheet = sprite.newSpriteSheet( “stick.png”, 100, 100 )

local heroSet = sprite.newSpriteSet(spriteSheet, 1, 11)

sprite.add(heroSet, “running”, 1, 10, 450, 0)
sprite.add(heroSet, “stop”, 11, 11, 1, 0)

local hero = sprite.newSprite (heroSet)
hero.x, hero.y = 100, 100

hero:prepare(“running”)
hero:play(“runnning”)

local function test ()
hero:prepare(“stop”)
end
timer.performWithDelay(1000, test, 1)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 22287 reply_id: 89232[/import]

It’s so weird, sometimes it stops in the standig position but other times it stops in a frame from the running set. [import]uid: 128340 topic_id: 22287 reply_id: 89233[/import]

Do you have some code showing that I could take a look at? [import]uid: 52491 topic_id: 22287 reply_id: 89255[/import]

The code I posted at the beginning, thats my whole 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”)

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”)

display.getCurrentStage():setFocus( nil )
Runtime:removeEventListener(“enterFrame”,run)
end
end

btnLeft:addEventListener(“touch”,btnLeft)
btnRight:addEventListener(“touch”,btnRight)
[/blockcode] [import]uid: 128340 topic_id: 22287 reply_id: 89323[/import]

Got it!

Change [lua]sprite.add(heroSet, “stop”, 11, 11, 1, 0)[/lua]
to;
[lua]sprite.add(heroSet, “stop”, 11, 1, 1, 0)[/lua]

I missed that before - the first number is the starting frame, the second number is how many frames you want played.

Then after each hero:prepare(“stop”) add hero:play(“stop”).

I believe that should do it :slight_smile: [import]uid: 52491 topic_id: 22287 reply_id: 89384[/import]

YESS!! it works now, thank you so much!, I’ll name my hero Peach now hahaha, I was about to give up, thank youuu!

G. [import]uid: 128340 topic_id: 22287 reply_id: 89408[/import]

Haha that might put off some players :wink:

Happy to help, I know it can be frustrating sometimes and make you want to give up but sticking with it you will ALWAYS get there in the end.

Peach :slight_smile: [import]uid: 52491 topic_id: 22287 reply_id: 89424[/import]