Cocos2d-iPhone Drag anywhere to move sprite - iphone

Ok what I would like to do is duplicate the controls of Space Invaders Evolution.
Begin a touch anywhere then drag around and the sprite moves 1:1 with your finger.
Sprite can not be moved of screen.
I have been using UIGestureRecognizer to handle dragging the sprite around the screen but im still pretty new to this and I have not been able to get this to work yet.

I don't know if you're unwilling to try this but it seems to me that this sort of thing could easily be done using ccTouchesBegan and ccTouchesMoved. I'd implement it by putting the following in my ccTouchesBegan and ccTouchesMoved methods (where sprite is the name of the sprite you're trying to move):
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
sprite.position = ccp(location.x, location.y);

Are you using Cocos2D or the Apple Frame-work?
In cocos2d you could use a layer (your scene for example) to detect the touch, which then moves the sprite. When the move is applied, check whether the sprite is still within screen boundaries, and move it back into screen boundaries if it's not.
There is example touch code in the cocos2d distribution. Not for your specific case, but general code, with text and explanation.

Related

how to move view with my finger in iphone Objective-c

i have this view that the size of him is 1280 X 345 and im moving it left and right in my screen.
now, my main question is how do i make it move with my finger (not swipeLeft / swipeRight) i want it to move with my finger like the home screen of the iPhone IOS.
now, im using this code:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:infoView];
//[self.view setTransform:CGAffineTransformTranslate(CGAffineTransformIdentity, (location.x + self.view.frame.origin.x - dx), 0.0)];
NSLog(#"touch: %f",location.x);
infoView.frame = CGRectMake(infoView.frame.origin.x + location.x, 0, 1280, 345);
}
that should be the current way but i cant figur it out.
i have also try to find an answer in google and here, but, as you know.. i didn't found something useful.
i've also made this so you can understand it better.
Don't write code to do this yourself. Use UIScrollView. This is what it is designed for.
If you want to have the same effect as the iPhone/iPad home screen you should use a UIScrollView and enable paging.
If you really want to handle it yourself for some reason, avoiding UIScrollView, there are UIGestureRecognizers, specifically UIPanGestureRecognizer which can leverage most of the job handling multiple touch events in one place.
i used the UIPagecontrol and UIScrollView to make it. very easy and smart object !

Objects on screen start blinking when dragging objects

I have an object that the user can drag around the screen in my app. But for some reason, when they start to drag it over another UIView, the items on screen start blinking. So part of the CAGradientLayer will appear in front of everything else, some things will seem to push themselves to the back, all sorts of bizarre activity. I haven't been able to take a screenshot of this unfortunately. This is the code that i've been using to drag the dragging.
NSSet *touches = [event touchesForView:sender];
UITouch *myTouch = [touches anyObject];
CGPoint startPoint = [myTouch locationInView:self.view];
positionX = startPoint.x;
positionY = startPoint.y;
colourDropView.center = CGPointMake(positionX-15, positionY-25);
colourDropView is the object that's being dragged, as you might guess. And it's when that last line is implemented when it starts the blinking, and it happens each time the user moves their finger. No other code is running when they drag their finger, only what is above.
Any ideas as to why this may be happening?
Turns out it's a bug. colourDropView had rounded corners, and had 'maskstobounds' set to YES. For some reason this causes a CAGradientLayer to freak out when this view is dragged over it.

How to detect the boundary of an uiimage

In my application i want to detect the boundaries of an uiimage. I'm having an flower image has many parts like,(lief,sticky bulb etc..)as a single image.If i'm touch the particular lief means it find the boundary value of that particular lief and return the value.
I'm having no idea about this.Please any one help me out to do this.
You can use "Flood Fill Algorithm" for that.
Only getting the touch points is not sufficient for this answer.
You need to break all in pixels and then you will use that further.
You could implement some edge detecion algorithm. But unless you have some experience in image processing I think that would be a major headache.
If you have the image in advance (do you?) you could separate the image as a composition of masks and detect the touches in the corresponding mask. The idea is that for each part of the image you will have a corresponding bitmap mask. When you receive a touch, you check all masks to see which one has a black pixel in the location of the touch. It is a thick Quartz technique, but I think it is much more approachable than the edge detection. Check out the relevant quartz documentation.
I think you can do this by subclassing the UIImageView and detecting the touchevent and position.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint position = [touch locationInView:self];
}
I havent tried this, but seems like this will solve your problem.

Detecting image touch (cocos2d)?

In cocos2d how would you detect a touch on an image? I'm having a lot of trouble with this so thanks in advance!
You implement the ccTouchesBegan/Ended/Moved methods within your Layer class, and then check the touch location against the container of the nodes you wish to detect touches for.
For example:
-(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch touch = [touches anyObject];
CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView: [touch view]]];
CGRect mySurface = CGRectMake(100, 100, 50, 50);
if(CGRectContainsPoint(mySurface)) {
// do something
return kEventHandled;
}
return kEventIgnored;
}
Now, this all changes in Cocos2D 0.8 (which is in active beta now) by using 'Touch Delegates' and examples can be seen in the 'Touches Test' (which appears to be a pong game from the source I just looked over).
I'm not sure why Corey said to use UIKit controls to detect touches, since Cocos2D has it's own way of handling them.
Only layers can receive touches - it is not advised that you use a Layer for each touchable 'game object' (ie; players and objects) ...
You need to overly invisible touch surfaces on top of the game using standard UIKit classes.
You then detect and interpret touches through those objects and pass the controls to your game.
If you have a more specific problem, you can provide more info or ask another question.
This post will give you the answer
Problem with cocos2D for iPhone and touch detection
Problem with cocos2D for iPhone and touch detection

How to center align a sprite?

In cocos2d does anyone know how to center a sprite? Right now, I have a sprite that moves to where you touch on the screen. The problem is that the sprite is aligned to the lower left corner. This means that instead of moving down, if you touch just a little over the bottom the sprite will move up. Thanks in advance!
Here is my code...
(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView: [touch view]];
[mainSprite do:[MoveTo actionWithDuration:0.5 position:ccp(point.x, 480 - point.y)]];
return YES;
}
The default transformation anchor for sprites in Cocos2D is the center of the sprite, so it should be moving such that the center of the sprite ends up in the touched location as you have it now. Have you changed the sprite's transform anchor?
The only other thing I can think of is that if your mainSprite is a child of another CocosNode then you may need to convert the touch coordinates to node space using this method:
- (CGPoint)convertToNodeSpace:(CGPoint)worldPoint;
...on the parent node. However, I doubt that is the problem. Sorry if this is unhelpful.
EDIT: OP, if you read this, what version of Cocos2D are you using? I believe 0.8 (currently in the svn trunk) changes the way that anchoring works; for future reference it may be useful to others to know what you're working with.
I got it working! For anyone that wants to know here is the code...
[mainSprite setTransformAnchor:ccp(24.0, 64.5)];
24 is half of the sprites width
64.5 is half of the sprites height