Rotate CCSprite around a specific point? - iphone

How can I rotate a sprite around a specific point, instead of the center point?
CCSprite rotation or CCRotateTo action rotate around the center point.
I'm trying to rotate the image of a face. Rotating around the center (nose) does not look as good as rotating around a point slightly below the center (chin).
In Photoshop it's possible to change the anchor point for rotation. Can that be done in cocos2d-iphone?

The most obvious way would be to enlarge the size of your sprite.
If the height is 50px, and the current center rotation point is the middle (25px) and you want it to be at say, 30px, just enlarge the canvas of your sprite in photoshop so that the image gained 10px of transparent space at the bottom.
Hopefully this helps your problem, i've never seen an anchor point reference for rotation in cocos2d.

You should change your anchorPoint accordingly.
AnchorPoint will be the center of your ccsprite rotation, it starts from left bottom (0.0,0.0) to right top (1,1). To calculate it, get the point position relative to ccsprite, and divide by the sprite size:
//anchorPoint position
float x = myRotationPosition.x/ mySprite.width;
float y = myRotationPosition.y/ mySprite.height;
mySprite.anchorPoint = cpp(x,y);
//rotate mySprite based on new anchorPoint
mySprite.rotation = 90;
Hope that helps

Related

Rotate a UIButton with an angle by a new anchorPoint

I'm trying to Rotate a button basically with a new anchorPoint. Normally anchor points are right in the center of the views. So I want to change the anchor point to right bottom of the frame of my Button.
I've tried to set an anchor point to the right bottom corner of the button frame and tried to rotate the whole button for 60 degrees to right hand side. It does rotate with the code below. But the rotation seems a bit random to me.
What I wanted was rotating a button with an anchor point(which is right bottom point of the button frame) and rotation angle is meant to be 60 degrees to the right hand side
So here is my basic code.
override func viewDidLoad() {
super.viewDidLoad()
let anchorPoint = CGPoint(x: 1, y: 1)
LoginScreenSelectionButton.layer.anchorPoint = anchorPoint // setting new anchor
LoginScreenSelectionButton.transform = CGAffineTransform(rotationAngle: 0.33)
}
My code seems logical and pretty easy to me. What is wrong about it?
I've added an image below. What I'm actually trying to do is drawing the shape below. I've drawn triangle images from adobe photoshop and added those photos to my button in swift.
Now I want to get a full circle from those triangle buttons. To do that, what I thought to do was creating each triangle button from the same point. So they will be on the top of each other.
To create a circular shape I wanted to change the angles of each triangle by adding 60 degree difference between each other(while the anchor point is the right bottom of the button frames).
With that logic I thought that I Would get a perfect circular shape which is created by 6 triangle shaped buttons. So by creating this shape I get 6 different buttons which seems to create a circular shape all together.
Is that approach logical as well?
EDIT: Sorry for long explanation hopefully that will give some information to you about what I'm trying to accomplish
Triangle Button shapes
Thanks

Does SKSpriteNode texture alpha influence touch area?

If a SKSpriteNode has a texture with alpha that's not rectangular, and no outline/border, does the touch area of the SPSpriteNode reduce to the area of only opaque pixels and ignore the completely transparent pixels?
Or is the SKSpriteNode's rectangular size, regardless of texture transparency, the touch area?
Yes and no believe it or not, touch area is based on the frame property of your node, This is the minimal visible boxed area of your sprite. So if you have a Sprite that is 32x32, but inside you have a visible circle of diameter 16, then your touch area is a 16x16 square that surrounds the circle

ios game make a mask layer effect

I need a 'mask' layer that covers the whole screen, with the center part (a circle) to be transparent. Then I can move the mask layer around using touch. User are only able to see the transparent part in the middle.
I don't think a png file can help because the file need to be very large to cover the whole screen.
So is it possible to do it by coding?
i found this online, but don't know much about openGL. http://www.cocos2d-iphone.org/forum/topic/7921.
it would be great if i can use a CCMaskLayer and input with the radius. i can handle the touch event by my self.
the attached png file is expected result, the center part is transparent. i need this to cover my screen, and only show the middle part. the red part is covered.
I write a CCMaskLayer to do the exactly same thing.
https://github.com/smilingpoplar/CCMaskLayer
You may solve this task with cropped circle texture in two ways:
1) Draw sprite with circle texture in screen center and draw another 4 sprites around (on top, bottom, left and right sides) with small red texture but scaled to cover all screen.
2) (more elegant but harder to implement) Make your mask layer fullscreen but adjust texture coordinates. In details:
set wrap mode to GL_CLAMP_TO_EDGE to your circle texture
adjust texture coordinates of your layer vertices (to do this you need to subclass base CCLayer):
Here v means vertex position and t - texture coordinates. You need to set correct texture coordinates for four corner vertices of layer. For future if you will want to drag circle you will need to add some offset values to texture coordinates.

Rotate and move UIView simultaneously and set its center point

I'm making a game which uses multiple UIViews for its displaying of content.
There are 2 UIViews. One of which is a map; another is a plane.
The plane's view is static. It doesn't move, rotate, scale etc.
I'd like to have the map translating under the plane, rotating at the same time. The map's origin (or center point) needs to be set as the plane's center point every frame.
How do I achieve this?
I'm using CGAffineTransform for this, and it works okay, until I want to turn round back on the map once I've left it.
Here's what I'm doing now:
CGAffineTransform oldTransformation = CGAffineTransformTranslate(CGAffineTransformIdentity, 0, frames);
oldTransformation = CGAffineTransformRotate(oldTransformation, rotation);
cityView.transform = oldTransformation;
Any help appreciated.
This blog here talks about using CAAnimationGroup to apply multiple animations (center point, rotation, and bounds) at the same time. It includes a sample application which you can get on github here. (Note: run the app in iPad simulator).
The animation used in that project combines the simultaneous change of center point and rotation of a view.
Hope this helps.

iPhone animation: how do I animate the filling up of an area?

Consider a circle. Now consider a radius of the circle, drawn from the center of the circle towards the right. Now imagine that the radius is rotating about the center of the circle, sweeping out an area as it rotates. My problem is: I want to use iPhone's animation techniques to fill up the swept out area with a different color from the background area of the circle, as the radius rotates from 0 degrees to any chosen number of degrees about the circle.
I've looked through the Core Animation API, KeyFrame Animations, etc. but I am not able to find out how to do this. Any hints will be most welcome.
Thanks,
Sandeep
Check out CAShapeLayer. It lets you animate between two Core Graphics paths, which can be as complex as you want. In this case, you might want to animate between a path which defines a thin filled wedge and a filled wedge that covers a larger angle of the circle. You may need to use a CAKeyframeAnimation to make sure that it is animated in the sweeping motion you desire, and in the direction you want.