Replacing image in sprite - cocos2d game development of iPhone - xcode

I want to change the sprite image.
Say for example:
mainSprite=[Sprite spriteWithFile:#"redFile.png"];
[self addChild:mainSprite];
Here, Sprite is already added to a layer. I have mainSprite (pointer) which can access it.
If I change
[mainSprite setOpacity:150];
it works perfectly. But here I want to change the sprite image instead of opacity.
But I don't know how.

#sagar:
In cocos2d 0.99.x I use
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"sprite.png"]];
It works. The next question is, how can I get back to my previous sprite? Thanks

Ok. Damn Simple.
I find it by R & D.
Texture2D *x=[[Texture2D alloc]initWithImage:[UIImage imageNamed:#"box-purple-dark.png"]];
[mainSprite setTexture:x];

Replace image in sprite :
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"image.png"]];

Replace image in sprite :
CCTexture2D *tex1 = [[CCTextureCache sharedTextureCache] addImage:#"new-image.png"];
[mainSprite setTexture:tex1];

Related

Why is my art displayed at 2x on screen?

I am creating a Universal App.
I have created my art in illustrator to fit properly on an iPhone screen, specifically at 640x1136 px. I exported PNGs and named myArt#2x.png and also a 50% version named myArt.png. The art is in an atlas folder.
I am creating sprite nodes a like this:
NSString * backgroundName=#"starField";
SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:backgroundName];
background.position = CGPointMake(screenWidth/2, screenHeight/2);
background.zPosition = LayerBackground;
[_worldNode addChild:background];
The problem is that when I run this in the simulator (iPhone 4 w/retina), the images are scaled up. I have to ousel xScale and yScale to bring it back to actual size.
Why? What am I doing wrong?
Thanks,
rich!
You need to set the size property of the SKNode I think. For example:
background.size = CGSizeMake(screenWidth, screenHeight);
Okay I think I solved it -
I don't start creating my content until viewWillLayoutSubviews had been called. The default SpritKit Template uses viewDidLoad, but I changed that to viewWillLayoutSubviews and everything looks fine now.
I think I was trying to create stuff before the view "sorted" itself out...
Not sure what the difference is but it worked.
-r

Cocos2D animation - cut scene into pieces

I found this animation http://youtube.com/watch?v=wt6gsIcNBOA (have a look at 1:46).
I can not find anything how this animation was accomplished. Basically it should look like the scene is cut into 3 different parts.
Any idea how to make a similar animation ?
its a "simple" CCTransitionSplitRows animation
CCTransitionSplitRows *transition = [CCTransitionSplitRows transitionWithDuration:1 scene:scene];
[[CCDirector sharedDirector] replaceScene:transition];
there are more of such fancy transition in cocos2d
see
http://www.pawapps.com/2011/07/30/tutorial-transitions-in-cocos2d/ for example

get CATransform3D from Projection and ModelView matrices

How to get CATransform3D from Projection and ModelView matrices
I have browse this question before,the url;but it wasn't absolutely precise; I want to know why it’s not precise;
I find that the rotate by x--axis and y--axis is opposite(I can't post image);the Trapezoidal(when rotate the calayer , it's like a Trapezoidal) is opposite,dose calayer rotate is not like opengl?but it's right when rotate by z--axis.
I want get the Angle and then rotate it twice opposite;how to get Angle by the opengl matrices?
I want play movie by QCAR; I can get opengl matrices,and I want to use UIView to playe movie and other(Familiar with the UIView),and make UIview like Texture.
I just post a example on QCAR forum, you can check the detail here
The idea is simple.
1.I don't use avassetreader to read the video pixel, instead I use AVPlayer and AVPlayerLayer which can play remote video file.
2.I need to convert opengl modelview matrix to CATransform3D so the AVPlayerLayer will attach on the trackable image.Thanks for Hammer on stackoverflow, he shared an example about how to make this.
3.I tried to render camera background use opengl and AVPlayerLayer at same time, but the performace is not good enough, so I use another calayer to render the camera background. There is a bug in 1.5.8 when getting camera frame, thank for andersfrank the problem was solved here.
I put my code on pastebin:
http://pastebin.com/kmeVZ7jy
There are still some problem need to solve, but I think this is a right approach.
PS. For question 3 in my original post, you can fix it by set
cameraLayer.contentsGravity = kCAGravityResizeAspectFill;
This will fix the problem that the aspect ratio of the camera image is different with the iphone screen's.
Update
All the problem are solved, please check my original post on vuforia and pastebin.

How to permanently leave a CALayer rotated at its final location?

I am trying to rotate a CALayer to a specific angle however, once the animation is done, the layer jumps back to its original position. How would I rotate the layer properly so that it stays at its final destination?
Here is the code that I am using
CABasicAnimation *rotationAnimation =[CABasicAnimation animationWithKeyPath:#"transform.rotation.z"]; //Rotate about z-axis
[rotationAnimation setFromValue:[NSNumber numberWithFloat:fromDegree]];
[rotationAnimation setToValue:[NSNumber numberWithFloat:toDegree]];
[rotationAnimation setDuration:0.2];
[rotationAnimation setRemovedOnCompletion:YES];
[rotationAnimation setFillMode:kCAFillModeForwards];
Any suggestions? Thank you.
Ok I fixed it by setting removeOnCompletion to NO.
[rotationAnimation setRemovedOnCompletion:NO];
You're adding an animation, but you aren't modifying the actual underlying property. After you create the animation, just set the layer's transform property to contain the same final result.

I am trying to clear the currently drawn image on the cgcontext

I have drawn a uiimage on a cgcontext
CGContextDrawImage(tempContext, CGRectMake(0,0,myRect.size.width,myRect.size.height), imgRef);
but once user is done with the current image, i want to clear the tempContext so that I can rescale / manipulate the image and redraw it on the same context.
for this i want to clear the the context and make it clean.
I tried CgContextClearRect but it didn't worked :(
try this one, it works for me: CGContextClearRect(UIGraphicsGetCurrentContext(), rect);

Resources