Im trying to build an app where i want the sprite image to flip to the right when gravity pulls it to the right.
The original sprite image is facing in the left direction and moves to the left when touched (anywhere on screen) through touches began.
If you're trying to rotate it 90 degrees then try this:
sprite.zRotation = M_PI/2.0f
So I'm thinking 180 degrees would be this:
sprite.zRotation = M_PI
Edit:
So if you want to invert the image try:
sprite.xScale = -1.0;
But if your node has any child nodes it will invert those as well.
Related
I placed a circle image in the middle of the screen and had the image move to the left. But it eventually stops so half of it still shows on the screen. I don't want to remove the circle, just move it a little bit further to the left so less of the circle shows. I tried setting the moveTo value to negative but it had the same effect as the value being 0. Any ideas?
SKSpriteNode *circle = [self makeSprite:#"circle" x:.5 y:.5];
circle.position = CGPointMake(self.size.width*.5, self.size.height*.5);
[circle [SKAction moveToX:0.0 duration:3]];
I'm learning how to use SpriteKit.
Question:
As practice, I'm trying to make a mock of pong. I want the ball's initial position to be in the center, and for the life of me, I cannot set its position to be in the center. It always ends up in the bottom left hand corner of the screen and I have no clue why. I've set the scene's scale mode equal to SKSceneScaleModeAspectFill, and I've tried each of the following lines of code in separate trials in order to get set its position to the center, but it has always remained in that bottom left hand corner.
//Attempt #1
self.ball.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)); //find the middle of each axis and put them together in one coordinate
//Attempt #2
self.ball.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); //find the width and height of the screen, divide each by 2 and assign them to an (x,y) coordinate together
//Attempt #3
self.ball.position = CGPointMake(160, 284) //Half the width and half the height
//Attempt #4
self.ball.position = CGPointMake(320, 568) //The width and the height (I'm desperately guessing at this point)
//Attempt #5
self.ball.position = CGPointMake(0,0) //I figured I'd give it a go
This ball is an SKSpriteNode by the way I didn't make a Ball subclass of SKSpriteNode or anything. The strange thing about this, is I've been able to bring up other sprites on the screen such as paddles and scores exactly where I want them. It's just this ball that isn't cooperating. What am I doing wrong? How can I prevent this issue in the future?
If you want to make ball's initial position to centre, then write below code:
[self.ball setPosition:CGPointMake(self.size.width/2, self.size.height/2)];
I'm using above code in my code and it is giving me perfect result.
Let me know it is working or not?
Happy Coding!
So here's the gist of what I'm trying to do here.
I have an array of foreground sprites that I scroll forever as the player moves along. What I would like to do, is when the player starts passing a certain point on the Y axis, scale down those foreground sprites while still moving them.
I'd like to be able to scale the sprites from their bottom left hand corners when the player is going up, and I've got this working without any problems.
The real problem is that I'd also like to scale the sprites from their bottom right hand corners when the player is coming down. Now I thought that I could do this by setting each sprite's anchor point to 1,0 before scaling it, but that doesn't seem to work. The sprites still scale from their bottom left hand corners.
What am I missing here?
// do logic to identify the scale factor we want
for (CCSprite *sprite in foreground_sprites)
{
CGPoint old_anchor = sprite.anchorPoint;
[sprite setAnchorPoint:ccp(1,0)];
[sprite setScale:scale_factor];
[sprite setAnchorPoint:old_anchor];
}
Have you tried messing with this property?
ignoreAnchorPointForPosition(false);
I'm using cocos2d-x, there should be something similar to that
If I understand correctly, you want to scale from the bottom left while the player's Y position increases but scale using the bottom right while they are descending?
Well you can't just change the anchor point alone. The anchor point and position go hand in hand to position the sprite on the screen. So if you positioned the sprite on to the screen using an anchor point of (0,0) then if you want to switch it's anchor point to (1,0) while keeping it in the same location on the screen, you'll need to update the position.
CCSprite* sprite = ...;
sprite.anchorPoint = CGPointZero;
sprite.position = CGPointZero;
...
sprite.anchorPoint = CGPointMake(1.0f, 0.0f);
sprite.position = CGPointMake(sprite.position.x + (sprite.contentSize.width * sprite.scaleX * sprite.anchorPoint.x),
sprite.position.y + (sprite.contentSize.height * sprite.scaleY * sprite.anchorPoint.y));
Hopefully I understood your problem correctly and was able to help.
I want to rotate a UIImageView, and I am using this code:
-(IBAction)rotateImageView:(id)sender{
photoView.transform = CGAffineTransformMakeRotation(M_PI); //rotation in radians
}
The code works. when I press the button, the image rotates 180 degrees, but if I press it again it doesn't rotate back to its original position, but it stays still. why??
Because you are applying the same transform to the view again, without taking into account any existing transform.
Try this:
photoView.transform = CGAffineTransformRotate(photoView.transform,M_PI);
you have to rotate the imageView from the current rotation to make it original
use this code
CGAffineTransformRotate(photoView.transform, M_PI);
I have a sprite :
ombreoeuf1 = [CCSprite spriteWithFile:#"mangeurcentremieu3_03.png" ];
ombreoeuf1.position = ccp(240,160);
[self addChild:ombreoeuf1];
And I would like to rotate it constantly around an anchor point. How can I do it?
You can first set anchor point by setting the property anchorPoint, for example:
[ombreoeuf1 setAnchorPoint:ccp(0,0)]
and then set rotation (in degrees) by setting another property rotation:
[ombreoeuf1 setRotation:90]
anchorPoint and rotation are both properties of CCNode class, which is the parent of CCSprite.
Update
According to your comments, it seems that what you want is a rotating sprite which never stops? Here is an example which let the sprite rotate 10 degrees per 0.1 seconds:
[sprite runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:0.1 angle:10]]];
All transformations of CCNode subclasses are done relatively to the anchor point. During all of your transformations the anchorPoint will have the same position. For example, if you will place sprite with anchorPoint (0.f, 0.f) to the position (0.f, 0.f), the left-bottom corner of the screen, then set it's scale, for example, to 5.f, after transforming it will stay at the left-bottom corner, just wil become larger. So all rotations automatically will be done relatively to the anchor point.
Just one more thing. CCSprite has anchorPoint (0.5f, 0.5f) by default and some content size, so you just have to set it to another to see changes in transformations. If you want to do it with CCNode, you have to set it's relativeToAnchorPoint property to YES and set contentSize manually.
You can use CCRepeatForever action for this. For example,
id rotateAction = [CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration: yourDuration
angle: anyAngleForGivenTime]];