draw line using finger and an object will follow that path? - iphone

I m new to game devloping in ios. Now I want made a game similar like "Control Air Flight" and "Air Traffic Controller" ,
where a user can draw line using their finger and an object will
follow that path
so ,anyone can guide me which is best for developing like this.Can Cocos2d is best for it? Or any thing else i have to use for this.
Also If anyone knows of a tutorial already out there or any reference link please suggest me.
Thanks in Advance.

To simply let the object follow your finger, implement touches (and just one of its methods):
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint toPoint = [touch locationInView:self.view];
[yourObjectOutlet setCenter:toPoint];
}
Here, your object's center will follow your path, but you can adjust its anchor point by editing "toPoint" accordingly to object's frame.
EDIT
If you want to draw the path, then make the object follow that path, do like this:
//define an NSMutableArray in your header file (do not forget to alloc and init it in viewDidLoad), then:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//you begin a new path, clear the array
[yourPathArray removeAllObjects];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint toPoint = [touch locationInView:self.view];
//now, save each point in order to make the path
[yourPathArray addObject:[NSValue valueWithCGPoint:toPoint]];
}
Now you want to start the move:
- (IBAction)startMoving{
[self goToPointWithIndex:[NSNumber numberWithInt:0]];
}
- (void)goToPointWithIndex:(NSNumber)indexer{
int toIndex = [indexer intValue];
//extract the value from array
CGPoint toPoint = [(NSValue *)[yourPathArray objectAtIndex:toIndex] CGPointValue];
//you will repeat this method so make sure you do not get out of array's bounds
if(indexer < yourPathArray.count){
[yourObject setCenter:toPoint];
toIndex++;
//repeat the method with a new index
//this method will stop repeating as soon as this "if" gets FALSE
[self performSelector:#selector(goToPointWithIndex:) with object:[NSNumber numberWithInt:toIndex] afterDelay:0.2];
}
}
That's all!

Related

a question about multitouch

I have several uiimages that I can move around on the iphonescreen using multitouches. The thing is that I want to separate them in two "teams" , a "team" of uiimages that I move inside an area of my choice and a "team" that I can move all over the screen.
My question is how to use the touch methods (touchesbegan, touchesended, touchesmoved) for both of the two uiimage "teams" and their cgpoints without the cgpoints from both "teams" crossing each other and giving wrong uiimages wrong positions on the screen. the 1st "team" uses the touchesbegan, touchesmoved and touchesended methods. the 2nd "team" only uses the touchesended method.
Here´s my code. I hope that the 2 "teams" don´t cross with each other in the touchesended method
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//1st team
for(UITouch *touch in touches){
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self getRubyAtPoint:[touch locationInView:self.view]forEvent: nil];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"touchesEnded");
//1st team
// Enumerates through all touch object
for (UITouch *touch in touches) {
// Sends to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:self.view]];
}
//2nd team
UITouch *touch = [touches anyObject];
CGPoint currentTouch = [touch locationInView:self.view];
[self getPieceAtPoint:currentTouch];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//1st team
NSLog(#"touchesMoved");
// Enumerates through all touch objects
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEvent:[touch view] toPosition:[touch locationInView:self.view]];
}
}
If I understand, you are asking how to keep the methods such as getPieceAtPoint from acting on the wrong team.
I think I'd just add a unique tag range to each team and check that before acting on it.
Something like:
UITouch *touch = [touches anyObject];
if ([touch view].tag>=TEAM_TWO_BASE_TAG)
{
CGPoint currentTouch = [touch locationInView:self.view];
[self getPieceAtPoint:currentTouch];
}

Animate object along a drawn path?

I recently downloaded the sample application, GLPaint from apple. This app showed me how to implement drawing, but now I would like to animate a image over the drawn lines. Similarly to how the application "Flight Control" works, I would like to be able to draw a path for an image and then have the image animate over this path.
any ideas?
you could create your path
CGMutablePathRef thePath = CGPathCreateMutable();
and then in the touchesBegan and touchesMoved add to your path
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch){
CGPoint tapPoint = [touch locationInView: self.view];
CGPathMoveToPoint(thePath, NULL,tapPoint.x,tapPoint.y);
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
if(touch){
CGPoint tapPoint = [touch locationInView: self.view];
CGPathAddLineToPoint(thePath, NULL,tapPoint.x,tapPoint.y);
}
}
and on touchedEnded, as Joshua said, create a CAKeyframeAnimation and set its path to thePath and set the other properties of your animation (duration, etc) and apply it to your object
Look at CAKeyframeAnimation, this allows you to set a path (which is a CGPathRef) for the animation to run along.
Based on code from AtomRiot, I created a simple project on my blog. See this post, also posted a short video showing the result.
However when the animation ends, the image will somehow repositioned back to (0,0), so might need to add extra code to reset the image location after animation finished.
Plus, found that you can't change the animation once it started, have to wait for it to end before you can change it.
Hope this helps!

move sprite by using touchmoved in specified regions of screen

I am making an application, in that I want to move the sprite in a specified region of the screen, With cocos2d I am not able to make move of sprite, I only know the method -(void)ccTouchMoved:(UITouch *)touches withEvent:(UIEvent *)event, but I don't know how to move a sprite,
can any one help me????
Thanks in advance
Try something like the following.
-(BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [[Director sharedDirector] convertCoordinate:
[touch locationInView:touch.view]];
[yourSprite setPosition:ccp(location.x , location.y )];
return kEventHandled;
}
Edit: If you simply want to move the sprite without a touch event simply call
[yourSprite setPosition:ccp(someX, someY)];

how do i get the location of a touch in cocoa/objective-c?

I am looking to get the location of a touch, preferably corresponding to the pixels of the screen. I'm new to objective-c and cocoa and haven't been able to find anything on the web about this so i'm not sure if there is even a way to do it. any ideas or direction of where to look would be really helpful. thanks.
On iPhone you make a subclass of the UIView and implement the funktion -(void)touchesBegan:(NSSet *)touches withEvent:(NSEvent *)anEvent:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [[touches allObjects] objectAtIndex: 0];
CGPoint currentPos = [myTouch locationInView: self];
NSLog(#"Point in myView: (%f,%f)", currentPos.x, currentPos.y);
}

Problem with cocos2D for iPhone and touch detection

I just don't get it.
I use cocos2d for development of a small game on the iPhone/Pod. The framework is just great, but I fail at touch detection. I read that you just need to overwrite the proper functions (e.g. "touchesBegan" ) in the implementation of a class which subclasses CocosNode. But it doesn't work. What could I do wrong?
the function:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{NSLog(#"tickle, hihi!");}
did I get it totally wrong?
Layer is the only cocos2d class which gets touches.
The trick is that ALL instances of Layer get passed the touch events, one after the other, so your code has to handle this.
I did it like this:
-(BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
CGPoint cLoc = [[Director sharedDirector] convertCoordinate: location];
float labelX = self.position.x - HALF_WIDTH;
float labelY = self.position.y - HALF_WIDTH;
float labelXWidth = labelX + WIDTH;
float labelYHeight = labelY + WIDTH;
if( labelX < cLoc.x &&
labelY < cLoc.y &&
labelXWidth > cLoc.x &&
labelYHeight > cLoc.y){
NSLog(#"WE ARE TOUCHED AND I AM A %#", self.labelString);
return kEventHandled;
} else {
return kEventIgnored;
}
}
Note that the cocos2d library has a "ccTouchesEnded" implementation, rather than the Apple standard. It allows you to return a BOOL indicating whether or not you handled the event.
Good luck!
Have you added this to your layers init method?
// isTouchEnabled is an property of Layer (the super class).
// When it is YES, then the touches will be enabled
self.isTouchEnabled = YES;
// isAccelerometerEnabled is property of Layer (the super class).
// When it is YES, then the accelerometer will be enabled
self.isAccelerometerEnabled = YES;
In order to detect touches, you need to subclass from UIResponder (which UIView does as well) . I am not familiar with cocos2D, but a quick look at the documentation reveals that CocosNode does not derive from UIResponder.
Upon further investigation, it looks like Cocos folks created a Layer class that derives from CocosNode. And that class implements the touch event handlers. But those are prefixed by cc.
See http://code.google.com/p/cocos2d-iphone/source/browse/trunk/cocos2d/Layer.h
Also see menu.m code and the below blog post article for more info on this:
http://blog.sapusmedia.com/2008/12/cocos2d-propagating-touch-events.html
maw, the CGPoint struct members x,y are floats. use #"%f" to format floats for printf/NSLog.
If you use the 0.9 beta of cocos2D it has a really simple touch detection for CocosNodes. The real beauty of this new detection is that it handles multiple touch tracking really well.
An example of this can be found here
http://code.google.com/p/cocos2d-iphone/source/browse/#svn/trunk/tests/TouchesTest
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//Add a new body/atlas sprite at the touched location
CGPoint tapPosition;
for( UITouch *touch in touches ) {
CGPoint location = [touch locationInView: [touch view]];
tapPosition = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:location]]; // get the tapped position
}
}
think this can help you....
-Make your scene conforms to protocol CCTargetedTouchDelegate
-Add This line to init of your scene:
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
-Implement these functions:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
//here touch is ended
}