I am creating a simple pong game in Xcode. I have a 2 player version and both paddles can be moved and function perfectly. The game is flawless except for the fact that you cannot move both paddles at the same time, rendering the 2 player mode useless.
How can I enable both paddles to be moved at the same time?
I've already tried selecting the "Multiple Touch" button in Interface Builder but it does nothing and im not quite sure it is even the correct route into enabling multi touch as I want it.
Also, my game is a View-Based Application if that matters.
Thanks!
EDIT: I mis-read the question. Added code snippet.
Here's the code I use to extract all touches when I get a touchesBegan event:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSArray *touchesArray = [touches allObjects];
NSUInteger nNumTouches = [touchesArray count];
UITouch *touch;
CGPoint ptTouch;
for (int nTouch = 0; nTouch < nNumTouches; nTouch++)
{
touch = [touchesArray objectAtIndex:nTouch];
ptTouch = [touch locationInView:self.view];
// Do stuff with the touch
}
}
and similarly for touchesEnded and touchesMoved
From my experience (which isn't that much). You can create 2 UIViews for the 2 paddles, touching in one view will move one paddle, while touching in the other will move the other paddle. I hope this helps.
If you don't know how to split the views, you can simply make it identify 2 touches
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch1 = [[event touchesForView:zone1] anyObject];
UITouch *touch2 = [[event touchesForView:zone2] anyObject];
if (touch1)
touchOffset1 = paddle1.center.x - [touch1 locationInView:touch1.view].x;
if (touch2)
touchOffset2 = paddle2.center.x - [touch2 locationInView:touch2.view].x;
}
This you can use, it probably isn't the most productive, but it does work if you can't figure out how to split the touches.
self.view.multipleTouchEnabled = YES;
by default it is No
Related
I am very new to Sprite Kit game development. I am currently developing my first game - a simple game where the player must navigate a simple object past obstacles. If the object collides with an obstacle - GAME OVER.
I got that to work fine but I have been stuck for over a week on an animation problem. I've literally searched for an answer for days now ... without success. So basically ... when my object is being navigated past the obstacles I have one animation pattern (that works fine). Once it hits an obstacle however, I want the animation pattern to change (into an explosion). And for some reason that is NOT HAPPENING! :( It is basically completely IGNORING the two lines of code ([self stopBeeAnimation]; and [self defeatedAnimation];) I added and the game is over right away.
Any help I might get on this, is really appreciated (this is driving me nuts) :)
Thank you so much.
Here my code:
-(void)didBeginContact:(SKPhysicsContact *)contact {
SKSpriteNode *firstSprite;
SKSpriteNode *secondSprite;
firstSprite = (SKSpriteNode *)contact.bodyA.node;
secondSprite = (SKSpriteNode *)contact.bodyB.node;
if ((contact.bodyA.categoryBitMask == beeCategory) && (contact.bodyB.categoryBitMask = obstacleCategory)) {
[self stopBeeAnimation];
[self defeatedAnimation];
[obstacleArray removeAllObjects];
[bee.physicsBody setAffectedByGravity:NO];
[timer invalidate];
scoreLabel.fontSize = 30;
scoreLabel.text = [NSString stringWithFormat:#"GAME OVER %d", score/2];
[self removeFromParent];
SKTransition *transition = [SKTransition fadeWithDuration:5];
[self.scene.view presentScene:[[NPMyScene alloc]initWithSize:self.size] transition:transition];
}}
and:
-(void) defeatedAnimation {
SKAction *defeatedAnimation;
NSMutableArray *textures2 =[NSMutableArray arrayWithCapacity:5];
for (int a = 1; a < 6; a++) {
NSString *textureName2 = [NSString stringWithFormat:#"defeated%d", a];
SKTexture *texture2 = [SKTexture textureWithImageNamed:textureName2];
[textures2 addObject:texture2];
}
defeatedAnimation = [SKAction animateWithTextures:textures2 timePerFrame:0.2];
}
Look here:
SKTransition *transition = [SKTransition fadeWithDuration:5];
[self.scene.view presentScene:[[NPMyScene alloc]initWithSize:self.size] transition:transition];
You're presenting a different scene (or maybe another instance of the same scene class?) in the view after trying to run your animations. Any animation changes in the current scene won't be visible because the new scene replaces the current scene.
But even before that...
- (void)defeatedAnimation {
// ...
defeatedAnimation = [SKAction animateWithTextures:textures2 timePerFrame:0.2];
}
You're creating an SKAction but not doing anything with it. For an action to take effect you need to run it on a node with runAction:.
I´m developing a game in which the player moves a toothbrush along the screen (dragging it). I want it to react when the left side of the toothbrush(the part of the toothpaste) touches another image(teeth) to "clean it". I've tried the next code but i dont get the best result,because the teeth reacts with the hole image of the toothbrush, even with the handle. I think the problem is the ".center"command. After searching in internet, i havent seen anything.
I would really appreciate your help.
#synthesize toothbrush, teeth;
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *myTouch =[[event allTouches]anyObject];
toothbrush.center=[myTouch locationInView:self.view];
[self collision];
}
-(void)collision {
if (CGRectIntersectsRect(toothbrush.frame, teeth.frame)) {
cont++; //when the two images collide, the "cont" variable increases its value
mensaje.text=[[NSString alloc]initWithFormat:#"%d",cont];
}
On TouchesEnded I want to do two things:
1) return the view to its original position - this works fine.
2) I want to play an audio sound if the touch ended on the YES vies and a different sound for the NO views.
How can I do this?
At this moment my sound for NO will play for every touch ended - and this is not good.
I am new to this so please explain the basics :) my current code below.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
yesletter1.frame = keepYesLetter1Place;
yesletter2.frame = keepYesLetter2Place;
yesletter3.frame = keepYesLetter3Place;
yesletter4.frame = keepYesLetter4Place;
notletter1.frame = keepNoLetter1Place;
notletter2.frame = keepNoLetter2Place;
notletter3.frame = keepNoLetter3Place;
notletter4.frame = keepNoLetter4Place;
NSString *path = [[NSBundle mainBundle]
pathForResource:#"no"ofType:#"wav"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
Solved this issue not the way i wanted but thing work now.
I have 2 different sounds for some objects one sound and the other another sound.
What i did is added a flag for each object and did if(flag1 == 0 || flag2 == 0 ect...) play sound 1 else play sound 2.
in another method which does something else i played with the flags.
I am sure there is a better solution for this :) but things work now.
I have three gestures: 2-finger tap, 3-finger tap, and 4-finger tap. I need to get coordinates accordingly.
I have tried the following to get the coordinated of 2 fingers tap but app keeps crashing:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSArray *twoTouch = [touches allObjects];
UITouch *tOne = [twoTouch objectAtIndex:0];
UITouch *tTwo = [twoTouch objectAtIndex:1];
CGPoint firstTouch = [tOne locationInView:[tOne view]];
CGPoint secondTouch = [tTwo locationInView:[tTwo view]];
NSLog(#"point one: %#", firstTouch);
NSLog(#"point two: %#", secondTouch);
[twoTouch release];
}
First of all, your application is not checking if there actually are two touches.
If you tap the screen with one finger you will get one touch in the "touches".
Try something like this.
if(touches.count > 1 && touches.count < 3)
{
// Your code for two touches.
}
Otherwise, the part where your program crashes is the [twoTouch objectAtIndex:1] because objectAtIndex:1 doesn't exist.
(I know this is a really old question but I answered it anyways.)
I have a CAShapeLayer and it has to do a simple task of moving on the screen, guided by the user's finger.
The problem is that the movement is too slow. The layer does move, but there is a lag and it feels slow.
I have another test app where an UIImage is moved and there is no lag at all and the image moves instantly.
What can I do to overcome this?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
currentPoint = [[touches anyObject] locationInView:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint activePoint = [[touches anyObject] locationInView:self];
CGPoint newPoint = CGPointMake(activePoint.x - currentPoint.x,activePoint.y - currentPoint.y);
curLayer.position = CGPointMake(shapeLayer.position.x+newPoint.x,shapeLayer.position.y+newPoint.y);
currentPoint = activePoint;
}
Thanks!
Keep in mind that when you set the position on a layer (assuming it's not the root layer of a UIView on which actions are disabled by default), it implicitly animates to the new position, which takes 0.25 seconds. If you want to make it snappier, temporarily disable actions on the layer like this:
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint activePoint = [[touches anyObject] locationInView:self];
CGPoint newPoint = CGPointMake(activePoint.x -
currentPoint.x,activePoint.y - currentPoint.y);
[CATransaction begin];
[CATransaction setDisableActions:YES];
curLayer.position = CGPointMake(shapeLayer.position.x +
newPoint.x, shapeLayer.position.y + newPoint.y);
[CATransaction commit];
currentPoint = activePoint;
}
This should cause it to jump to the new position rather than animate. If that doesn't help, then let me take a look at your layer init code so I can see what properties and dimensions it has. Properties such as cornerRadius, for example, can affect performance.
Try setting shouldRasterize to YES on your CAShapeLayer, particularly if it is usually drawn at the same scale. If your app runs on high-DPI devices, you may also need to set rasterizationScale to match the layer’s contentsScale.
While rasterizing your shape can make it faster to move the shape around, you’ll probably want to temporarily disable rasterization while you’re animating the layer’s path or size.