how to detect collision of image views in a puzzle game - iphone

i am doing a simple jigsaw puzzle game.
for that i crop a single image into 9 pieces and display it on 9 image views.
Now i need to detect collision when one image view is comes over the half of another image view frame,
and replace image or image view each other.
how can i done can any one please help me.

You can use the CGRectIntersectsRect() function, it takes to CGRect's and returns YES if the rects intersect, otherwise NO.
Here is a short example:
if(CGRectIntersectsRect(image1.frame, image2.frame))
{
UIImage *temp = [[image1 image] retain];
[image1 setImage:image2.image];
[image2 setIamge:[temp autorelease]];
}
(It works of course easier if you have an array to iterate through)

Related

SpriteKit reducing SKLabelnode draw calls

So I have a scene in my game which displays the levels, like any other game with level, I subclass SKSpriteNode to make a custom level button and within this subclass i Add a SKLabelNode to display the level title ( level 1, level 2 .....). The problem know is that i have a lot of draw calls because each SKLabelNode renders as one texture instant of combining them into an atlas. I would like to know if someone can help me reduce these draw calls. I dont want to use Glyph designer because this game is going to be in a lot of different languages like Japanese Chinese and more.
Any advice?
-(void)setText: (NSString *)text{
_label = [SKLabelNode labelNodeWithFontNamed:#"CooperBlack"];
_label.text = text;
_label.fontColor = [UIColor blackColor];
_label.fontSize = 11;
_label.zPosition = 2;
_label.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
_label.position = CGPointMake(0, 0);
[self addChild: _label];
}
Depending on what you're doing and when, you could render out the contents of the labels into textures at runtime (pre-loading / caching), and then manipulate them in any ways you'd like.
SKLabelNode *theThingToBecomeATexture;
//OR
SKSpriteNode *theThingToBecomeATexture;
SKTexture *theTexture = [theView textureFromNode:theThingToBecomeATexture];
But my follow up question or comment would be: I have a difficult time believing that you are running into performance problems by showing a few dozen label nodes on the screen. I can understand you hitting a load spike if you are trying to alloc and init a number of them all at the same time, in which case, I would preload them, or alloc them not on the main thread.

How to flip a CGImageRef over y-axis

I'm trying to animate a sprite using only CoreAnimation. It's working so far except I can't figure out how to flip the sprite sheet.
Right now, I have a walking animation, but I want the sprite to face the direction it's walking (because it looks kind of silly walking backwards).
I guess I could add the reversed images on the sprite sheet, but I would rather avoid that because it could make it really big when I decide to add more.
Right now, my sprite is extending CALayer and I've set its contents to the CGImageRef which is the sprite sheet:
self.contents = (id) image;
To flip it I tried:
UIImage *tmp = [UIImage imageWithCGImage:image scale:1.0 orientation:UIImageOrientationUpMirrored];
CGImageRef flippedImage = tmp.CGImage;
self.contents = (id) flippedImage;
..and that's not working.
I found other solutions which involve animating, but I don't want to animate the flip. I just want it to happen instantly.
Is there a simple way to do this?
If there's a way to flip the whole CALayer, I'd like to know that too. =]
Thanks!
Rather than flipping your image, you could try applying a transform to your layer object.
Something like:
self.transform = CATransform3DMakeScale(-1, 1, 1);
This may be better for performance too; if the sprite needs to walk in the opposite direction, you can just set your transform back to CATransform3DIdentity rather than allocing a new image and rotating it.

Move the image around another image

I need to move the one image around other image where both images are in circle shape,they should not collide or overlap with each other. I tried with CGRectIntersectsRect but no use of it because of corner radius of image i.e intersect function get called before they collide.
You can do this with animation but for this you should take it as single image as shown in 1st image and make different image with different position of blue image in circle.
loadingImageView.animationImages = [[NSArray alloc]initWithObjects:[UIImage imageNamed:#"circle1.png"],[UIImage imageNamed:#"circle2.png"],[UIImage imageNamed:#"circle3.png"],[UIImage imageNamed:#"circle4.png"],[UIImage imageNamed:#"circle5.png"],[UIImage imageNamed:#"circle6.png"],[UIImage imageNamed:#"circle7.png"],[UIImage imageNamed:#"circle8.png"],[UIImage imageNamed:#"circle9.png"],[UIImage imageNamed:#"circle10.png"],[UIImage imageNamed:#"circle11.png"],[UIImage imageNamed:#"circle12.png"],[UIImage imageNamed:#"circle13.png"], nil];
if(![loadingImageView isAnimating])
{
loadingImageView.animationDuration=4;
[loadingImageView startAnimating];
}
circle1.png,circle2.png,circle3.png... etc are images which contain blue and red image as one image with different position of blue image in circle. Now hope if will helpful for you.If any problem is there then tell me.

Face Detection issue using CIDetector

I'm working on an app in which i have to detect left eye, right eye, and mouth position.
I have an imageView on my self.view and imageView contains a face image, now I want to get both eyes and mouth coordinates. I have seen 2-3 sample codes for this but all are approximately same in all codes we have to invert my view for matching the coordinates which I don't want because my view have some other controls. And one more thing they all are using
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"image.png"]];
but my imageView has frame and i cant init it with image. When I do so I found faceFeature's eyes and mouth coordinates wrong.
I had started my code from this sample code but in this also view is being invert its Y coordinate.
Can any one help me how can i detect the face eyes and mouth position on UIImageView's image without invert my self.view.
Please let me know if my question is not clear enough.
The trick here is to transform the returned points and bounds from CIDetector to your coordinates, instead of flipping your own view. CIImage has origin at the bottom left which you will need to transform to the top left
int height = CVPixelBufferGetHeight(pixelBuffer);
CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);
transform = CGAffineTransformTranslate(transform, 0, -1 * height);
/* Do your face detection */
CGRect faceRect = CGRectApplyAffineTransform(feature.bounds, transform);
CGPoint mouthPoint = CGPointApplyAffineTransform(feature.mouthPosition, transform);
// Same for eyes, etc
For your second question about UIImageView, you just have to do
imageview.image = yourImage
After you have initialized your imageview
Worked it out! - edited the class to have a faceContainer which contains all of the face objects (the mouth and eyes), then this container is rotated and thats all. Obviously this is very crude, but it does work. Here is a link, http://www.jonathanlking.com/download/AppDelegate.m. Then replace the app delegate from the sample code with it.
-- OLD POST --
Have a look at this Apple Documentation, and slide 42 onwards from this apple talk. Also you should probably watch the talk as it has a demo of what you are trying to achieve, it's called "Using Core Image on iOS & Mac OS X" and is here.

Image stretching

I want Stretch a image. For that i use sprite. I want stretch sprite & this stretching is may be Circular or curve animation. I don't understand what methode used for that. Can anyone help me?
Since you tagged your question with cocos2d I guess you'll be using that. It's really basic to strech an image
Sprite *mySprite = [Sprite spriteWithFile:#"mysprite.png"];
mySprite.position = ccp(100, 100);
mySprite.scale = 2.0;
[self addChild:mySprite];
If you want to animate it you can use the cocos2d actions or just create your own animation. The example below does a linear animation to 3x original sprite size in 1 second:
id action1 = [ScaleTo actionWithDuration:1.0 scale:3.0];
[mySprite runAction: action1];
For manipulating views and images in general in ways such as streching you can read up on transforms provided by the sdk, you can learn about 2D transforms here http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_affine/dq_affine.html and you can extend that further to 3D by manipulating the layers transforms instead of the views transforms. Youll be able to do things such as scaling and rotating and you can define your own transforms as well. This example project http://developer.apple.com/iphone/library/samplecode/MoveMe/ is a good reference to get started with transforms and animating them.