Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Problem with sprites methods "setFrame" and "play"
Started by lealmas10 Nov 27 2018 08:14 PM

3 replies to this topic
sprite setframe play spriteobject frames
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

lealmas10

[GLOBAL: userInfoPane.html]
lealmas10
  • Observer

  • 2 posts
  • Corona SDK

Hello,

So, I need to play a sprite when the app starts, but from a frame different than 1. (ex. the 30th frame)

I tried using Object:setFrame(30) and Object:play(), but it doesn't work, the sprite goes to the frame 30

but doesn´t play it (Tried many frame numbers and only with 1 it plays).

 

This is the code:
 

-- LOAD THE SPRITE-SHEET
local sheetInfo = require("img") -- from texture packager
local mySheet = graphics.newImageSheet( "img.png", sheetInfo:getSheet() )
local mySprite = { name="mySprite", start=1, count=131, time=1000, loopDirection = "bounce" }

-- CREATE OBJECT
local myObject = display.newSprite( mySheet, mySprite )
myObject.x = display.contentCenterX
myObject.y = 400
myObject:setFrame(30)
myObject:play()

By using try and error I found that It plays when I call it from a listener, but I have to tap something to play it, and I need it to play when the app starts.

-- LOAD THE SPRITE-SHEET
local sheetInfo = require("img") -- from texture packager
local mySheet = graphics.newImageSheet( "img.png", sheetInfo:getSheet() )
local mySprite = { name="mySprite", start=1, count=131, time=1000, loopDirection = "bounce" }

-- CREATE OBJECT
local myObject = display.newSprite( mySheet, mySprite )
myObject.x = display.contentCenterX
myObject.y = 400

local function playSprite()
   myObject:setFrame(30)
   myObject:play()
end

myObject:addEventListener("tap", playSprite)

If I only put playSprite() in the end to start the function it doesn't play neither.

Can someone help?



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 368 posts
  • Corona SDK

I'm not sure if the answer is something as simple as forgetting to set the sequence before playing.

Try adding the following line before myObject:play().

 

myObject:setSequence( "mySprite" )


[TOPIC: post.html]
#3

lealmas10

[GLOBAL: userInfoPane.html]
lealmas10
  • Observer

  • 2 posts
  • Corona SDK

Hi, thanks for the reply

I think the sequence was set in display.newSprite( mySheet, mySprite )

And if I put it again after myObject:setFrame(30)  then the sprite plays from frame 1, the sprite gets reset.

And if I put it before myObject:setFrame(30) is the same problem

 

I finally solved it (if you can call it that) by making several sequences, in example, by going frames={30,31,...,130,131,1,2,...28,29}

Anyway I know there must be a easier solution.



[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 368 posts
  • Corona SDK

That's not enough to set the sequence. If you have only a single sequence, then it may default to the only one, but I can't remember for sure.

If you would be willing to share the part of your project that demonstrates the problem and upload it, then I'm sure that I, or others, could help you solve the problem for good. Trying to solve problem specific issues without the access to said project isn't always reliable.




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: sprite, setframe, play, spriteobject, frames