sprite.newSprite( spriteSet ) adds sprite to stage automatically?

Hi,

How can I make a sprite but not have it added to stage automatically?

Since sprite.newSprite() does not take parentGroup and I’m pretty sure it automatically adds the sprite to the stage.

I wanted to add the sprite to a displayGroup and not the stage.

Please advice. Thanks! [import]uid: 22047 topic_id: 5406 reply_id: 305406[/import]

Hey Paul.

I don’t know the answer, but I’ve got the junglescene open on my computer so I thought I’d paste this in for you to see if it can help you

[lua]-- A sprite sheet with a cat
local sheet1 = sprite.newSpriteSheet( “img/runningcat.png”, 512, 256 )

local spriteSet1 = sprite.newSpriteSet(sheet1, 1, 8)
sprite.add( spriteSet1, “cat”, 1, 8, 1000, 0 ) – play 8 frames every 1000 ms[/lua]

Taken from

http://developer.anscamobile.com/content/jungle-scene [import]uid: 14327 topic_id: 5406 reply_id: 18121[/import]

Thanks for the reply, but the sample shows the exact problem i’m tying to solve…
As you can see from the code here

local instance2 = sprite.newSprite( spriteSet2 )  
instance2.x = 3 \* display.contentWidth / 4 + 30  
instance2.y = baseline - 55  
   
instance2:prepare("man")  
instance2:play()  

the sprite called instance2 was never added to anything since it is added to the stage automatically by when you call sprite.newSprite(spriteSet2). This prevents from being able to control the layering of sprites properly.

[import]uid: 22047 topic_id: 5406 reply_id: 18139[/import]

but the point of newSprite is to add it to screen, the same as newImage.

if you set it’s alpha to zero, or visible to false, or x offstage, directly after calling newSprite i’m sure you won’t see it. [import]uid: 6645 topic_id: 5406 reply_id: 18161[/import]

Yes, you are right… I do want to add it to the screen.

But i do also want to control which ‘layer’ of the screen the sprite gets added to.
It could be a background tile map or a foreground animated sprite.

if i could do this then i’d be happy

[code]

– these 2 are added automatically to the stage in this order
local bgLayer = display.newGroup();
local fgLayer = display.newGroup();

– i can set up the background and the environment in 1 layer
local skySrite = sprite.newSprite(bgLayer, envSpriteSet)
– without interring with the stuff on this layer
local animatedCreature = sprite.newSprite(fgLayer, creatureSpriteSet)
– but newSprite wont take a parentGroup param :frowning:
[/code] [import]uid: 22047 topic_id: 5406 reply_id: 18162[/import]

[lua]local bgLayer = display.newGroup()
local fgLayer = display.newGroup()

local skySrite = sprite.newSprite(envSpriteSet)
bgLayer:insert(skySprite)

local animatedCreature = sprite.newSprite(creatureSpriteSet)
fgLayer:insert(animatedCreature)[/lua] [import]uid: 1294 topic_id: 5406 reply_id: 18198[/import]

@ludicrious

Thanks for the reply… :slight_smile:

Please correct me if i’m wrong, i think by end of line 4 of your example, skySprite is already on the stage. At least that’s what i’m seeing. Hence the post.

If line 4 doesnt add skySprite to the stage, then i’m wrong and problem solved.

Unless, adding it to bglayer, resets the parent to bglayer? [import]uid: 22047 topic_id: 5406 reply_id: 18210[/import]

have you read the docs? :stuck_out_tongue: [import]uid: 6645 topic_id: 5406 reply_id: 18250[/import]

http://developer.anscamobile.com/reference/index/groupinsert

(Ah, see jmp909 had the same idea :slight_smile: [import]uid: 1294 topic_id: 5406 reply_id: 18251[/import]

@ludicrous @jmp909

No, I haven’t read that page of the doc.
Thanks for pointing that out.

That should solve my problem :slight_smile:
[import]uid: 22047 topic_id: 5406 reply_id: 18256[/import]