I would like to know how to change the z-order of a sprite in cocos2D on touch.
What I am looking for is something more complex then just a z-order change from 0 to 1 on touch, but for it to then change from 1 to 2 and 3 to 4 and so on.
So if a sprite is on z:0 to start with on each subsequent touch it will go up by 1. (e.g. 1, 2, 3 ...)
use CCNode reoredChild
http://www.cocos2d-iphone.org/api-ref/0.99.5/interface_c_c_node.html#ab58888d399872a970aba610c1634a117
Related
I have a sprite animation of 30 frames, 6 of these frames are keyframes and the rest are transitions from keyframe to keyframe.
I am trying to figure out how to do the following:
When button is pressed the sprite starts animating. Animation will run at least once and then it will stop the animation on a randomly chosen keyframe.
When button is pressed again, animation will start from the last chosen keyframe and will run 1 complete cycle and then stops again on a randomly chosen keyframe.
I've found a similar question here but that was asked in 2014. Did Swift and Spritekit change enough in 3 years to make it possible?
Edit:
#Alessandro Ornano
I've tried this:
var roll1Textures = ["Roll1Motion0", "Roll1Motion1", "Roll1Motion2", "Roll1Motion3", "Roll1Motion4", "Roll1Motion5", "Roll1Motion6", "Roll1Motion7", "Roll1Motion8", "Roll1Motion9", "Roll1Motion10", "Roll1Motion11", "Roll1Motion12", "Roll1Motion13", "Roll1Motion14", "Roll1Motion15", "Roll1Motion16", "Roll1Motion17", "Roll1Motion18", "Roll1Motion19", "Roll1Motion20", "Roll1Motion21", "Roll1Motion22", "Roll1Motion23", "Roll1Motion24", "Roll1Motion25", "Roll1Motion26", "Roll1Motion27", "Roll1Motion28", "Roll1Motion29"]
let roll1Animation = SKAction.animate(with: Array(roll1Textures[0...5]), timePerFrame: 0.06, resize:false, restore:false)
But I get an error message: Cannot use instance member 'roll1Textures' within property initialiser; property initialisers run before 'self' is available.
How did I implement your code in a wrong way?
I don't know if it could be useful to your game but now in Swift you can also do in one line:
let animation = SKAction.animate(with: Array(textures[0...5]), timePerFrame: 0.06, resize:false, restore:false)
In other words you can create an array slice composed by your specific range then re-create the array of textures with Array
this one is my sprite sheet i want to change the image according to user moves his finger on the screen
i.e, when he is touches the finger near this bow which is displayed initially no2 image from sprite sheet must be displayed, as he goes on dragging his finger according images must be displayed on screen
can anyone please guide me on this scenario because i totally don't know from where i must start.
thanks in advance
this is how i am displaying first image from sprite sheet
danceSheet = [CCSpriteBatchNode batchNodeWithFile:#"bowspritesheet.png"];
[self addChild:danceSheet z:1];
danceSprite = [CCSprite spriteWithTexture:danceSheet.texture rect:CGRectMake(0, 0, 90, 140)];
danceSprite.rotation=-90;
danceSprite.position=ccp(screenSize.width/1.2, screenSize.height/6);
[danceSheet addChild:danceSprite z:0];
Here you have to make one variable that tells you that is the maximum distance user can stretch. Beyond that if he stretch , there will be no effect on the bow. Suppose that distance as 30 pixels.
Now you can implement your functionality using cctouchbegun method and cctouchmoved method. In cctouch method you have to set one ccpoint variable which will store the first location user touched. Suppose that as firstLocation. Simultaneously, you change the sprite of your bow to no.2 image.
Now, you have to work in ccmoved function. In that as user moved his fingure you will get current position where user has touched right now. So you have to get the distance and devide it by (30/10) that is 3. Suppose (your distance/3)= 1 then you don't need to change the image of the bow. Now if it is equal to 2 then you have to change the bow wiht no.(2+1=)3 image. SO From this way you can implement bow functionality.Here we did (30/10) because you have set maximum distance user can stretch is equal to 30 and you have 10 different image of bow with arrow.
Hope, you get this,.If you find any difficulty in implementing this you can as me.
I think you want some arrow animation using this given sprite-sheet, and you might want like when you swipe backwards from middle of your bow weapon then you want to launch your arrow. Right ?! you can use this sprite-sheet ,i guess what you have to do is just calculate or assume some distance. and as i am showing your this sprite-sheet , you have around 10 images for launching arrow action. so what you have to do is just put all those images at some equal distance according to your touch location ... i.e if you have 30px distance then you can put all your images according to your touch at 3px position. means you will start with your initial image at 0px & then put all other images at 0 -3 -6 -9 & so on.. so this way you might achieve this. I hope my this try help you .. ask me if you don't get my answer at any point.
I have some sprites that I've added to a CCSpriteBatchNode. Everything shows up fine. Now I want to see their bounding boxes.
I've tried going into ccConfig and turning on CC_SPRITE_DEBUG_DRAW but all I get are white squares for everything: the tiles I use for the level, the characters, etc.
I can't use the draw method for the sprites as they are being drawn through the batch node and it never gets called.
I'm using cocos2d-iphone v2.0.
So is there a way to draw a bounding box for a sprite that's being drawn via the batch node?
In ccConfig.h change
#define CC_SPRITEBATCHNODE_DEBUG_DRAW 0
to
#define CC_SPRITEBATCHNODE_DEBUG_DRAW 1
Hope this helps!
p
I am wondering how can I add a border & background to labels generated via CCLabelBMFont class in cocos2d.
I don't want to use sprites because my labels are generated on the fly and will keep changing and the size of labels are also different.
Also, I want user to touch and move these labels across the screen. When user picks a label it wiggles a bit like in free air. In that case, I wish to keep low complexity and preserve memory and cpu calculations.
Anyone knows best ways to achieve this?
IOS app LetterPress has similar effects.
Create your own class, that will incapsulate creation of complex node.
It will have several layers, for example, the first layer can be simple CCLayerColor of given rect with zOrder -2, the next layer will be your CCLabelBMFont with zOrder -1 and then you can overload draw method to draw border over your control. All that you draw in this method will be drawn with zOrder 0.
Then you can encapsulate any effects in this class. For example, you can rotate it a bit with method pick, etc. Whatever you want.
I have created a game of two levels.Now when we are in level one i create sprite sheet for animation and a lot of sprites.On reaching a certain score i move to level 2 now here here is another sprite sheet and a lot of variables.
When i am moving from level 1 to level 2 using.
[[CCDirector sharedDirector] pushScene:[Level2 node]];
when i lose on level 2 i move back to level 1 using
[[CCDirector sharedDirector] pushScene:[Level1 node]];
What happens to the sprite sheet and other sprites i created before on level 1 ? If i retry level 1 will the sprite sheet and sprites i created before be removed automatically ?or they will exists in this new scene?
kindly clear me these issue i am having a lot of trouble because of no understanding of this..
thank you in advance.. :(
In cocos2d almost everything is marked as autorelease. So when you create a new scene it is not released because it becomes the main scene. Once it is no longer the main scene, if you do not specifically retain it then it will be released.
In the case you state above I believe that both scenes are retained as you are using the director like a stack, pushing the scenes on each other. If you never need to go back to level 1 you can use CCDirector's replaceScene: method instead to release level 1.
Having a custom pause scene or bonus round might be a good example of when to use the pushScene: call.