Unable to apply impulse twice - ios

I'm trying to create objects and apply an impulse on them
There is no problem with the first time, but it looks I have to wait until the last one is removed in order to apply an impulse to the new object. It is created but the impulse is not applied
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchlocation = [[touches anyObject] locationInNode:self];
MySpriteNode *object = [MySpriteNode node];
object.position = touchlocation;
[self addChild:object];
[object.physicsBody applyImpulse:CGVectorMake(0.0, 9.0)];
[self performSelector:#selector(removeObject:) withObject:object afterDelay:1.5];
}

It seems the problem comes creating the physicsBody like this
[SKPhysicsBody bodyWithTexture:texture size:object.size];
The first time, the impulse is really strong, and the next ones really soft. So it looks it doesn't work if you compare with the first impulse.
I really hope it's a bug if the iOS 8 Beta

Related

How define location of CALayer in the UIView during movement

I have want to make an application where apples fall down from the tree and user can catch it with the basket. Basket starts moving when I touch the screen so it moves in that direction - but slowly. For this I use
-(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch* t = [touches anyObject];
CGPoint p = [t locationInView:self.view];
if(p.y <self.view.frame.size.height-50)
{
p.y = self.view.frame.size.height-50;
}
CABasicAnimation *basketmovementAnimation = [CABasicAnimation animationWithKeyPath:#"position"];
[basketmovementAnimation setFromValue:[NSValue valueWithCGPoint:[[self.basketLayer presentationLayer]position]]];
[basketmovementAnimation setDuration:3.0];
[self.basketLayer setPosition:p];
[self.basketLayer addAnimation:basketmovementAnimation forKey:#"basket"];
}
So the question is how to get coordinates of "basket" during movement? I need it to compare with the coordinates of fallen apples.
Thanks in advance
CALayer has a method for this.
- (id)presentationLayer
From the CALayer Class Reference
The layer object returned by this method provides a close
approximation of the layer that is currently being displayed onscreen.
While an animation is in progress, you can retrieve this object and
use it to get the current values for those animations.

Spritekit how to run an SKAction depending on where a touch and hold is at

So I'm trying to implement a method into a game where if a player has their finger touching the screen at lets say 50 on the y axis or lower, an SKAction will run to move the character sprite down, and if their finger is touching above the 50, another action will run moving the sprite up. I don't know how you have it recognize a touch and hold though. Help please.
You will need to capture the touch event of the user in order to pull this off. I suggest checking out the excellent tutorial provided by RayWenderlich at: Animating Textures and Moving Them With Touch Events
If you don't have time for that this is what your code might look like:
First the knowledge that you will need is that the two most generic touch events are the:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
and the:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
Basically the touches began method is called when the user places their finger on the screen whereas the touches ended method will be triggered when the user takes their finger off the screen.
For this example I will use touchesEnded, but feel free to change it if you want, its as easy as switching the "Ended" with "Began".
All of the code I am about to show you will take place within the scenes implementation file:
If you just want the sprite to move up when above a certain point and down when below a certain point use the following:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//capture the location of the touch
CGPoint location = [[touches anyObject] locationInNode:self];
//used to hold the location to travel
CGPoint moveLocation;
//check if the Y location is less than 50
if(location.y < 50)
{
//the the location to the bottom of the screen
moveLocation = CGPointMake(sprite.position.x, 0);
}
//and then if its more than 50
else
{
//set the locaiton to the top of the screen
moveLocation = CGPointMake(sprite.position.x, self.frame.size.height);
}
//move the sprite
//Check if their already moving
if(sprite actionForKey:#"moving")
{
//If they are stop them so they can move in the new direction
[sprite removeActionForKey:#"moving"];
}
SKAction *moveAction = [SKAction moveTo:location duration:5.0f];
//remember to give the action a key so that we can check for it during the above if statement
[sprite runAction:moveAction withKey:#"moving"];
}
And there you have it. The only flaw is that it will always take 5 seconds to reach the location regardless of your distance from it, read on if you would like hints on how to correct that, or if you would like to see how to make the sprite travel to any touched location on the screen.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//capture the location of the touch
CGPoint location = [[touches anyObject] locationInNode:self];
//move the sprite
//Check if their already moving
if(sprite actionForKey:#"moving")
{
//If they are stop them so they can move in the new direction
[sprite removeActionForKey:#"moving"];
}
SKAction *moveAction = [SKAction moveTo:location duration:5.0f];
//remember to give the action a key so that we can check for it during the above if statement
[sprite runAction:moveAction withKey:#"moving"];
}
That code will make it so that for 5 seconds the sprite will move to the location touched on the screen. Just remember to replace the instances of "sprite" with the name of your SKSpriteNode variable. For more complex movement try the following:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//capture the location of the touch
CGPoint location = [[touches anyObject] locationInNode:self];
//set the velocity of the subject
float velocity = self.frame.size.width/3.0;
//The above will ensure that it will take the sprite 3 seconds to travel the distance of the screen
//Determine the difference between the point touched and the sprites position
CGPoint moveDifference = CGPointMake(location.x - sprite.position.x, location.y - sprite.position.y);
//Use pythagorean theorem to figure out the actual length to move
float distanceToMove = sqrtf(moveDifference.x * moveDifference.x + moveDifference.y*moveDifference.y);
//Use the distance to travel and the velocity to determine how long the sprite should move for
float moveDuration = distanceToMove / velocity;
//and finally move the sprite
if(sprite actionForKey:#"moving")
{
[sprite removeActionForKey:#"moving"];
}
SKAction *moveAction = [SKAction moveTo:location duration:moveDuration];
[sprite runAction:moveAction withKey:#"moving"];
}
That will set a velocity for the sprite, determine the length of the travel, and how long it will take to get to the new location.
If you want to involve textures I highly suggest reading the link I provided.
Upon request I would be glad to offer examples using forces to control movement speed as well.
I hope that helps, let me know if there are any problems I wasn't in a position where I could run the code but I am sure that it is fine.

Determining whether a point lies within the boundary of a physics body in Sprite Kit

I'm using Sprite Kit to create a game involving balloons. The balloon image is a long rectangle (dotted line) but I've defined a physics body around the balloon itself using a polygon (solid line). I'd like players to be able to "pop" the balloon. I'm using the following block of code to achieve this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSArray *nodes = [self nodesAtPoint:[touch locationInNode:self]];
for (SKNode *node in nodes) {
[node removeFromParent];
SKAction *pop = [SKAction playSoundFileNamed:#"pop.wav" waitForCompletion:NO];
[self runAction:pop];
}
}
Unfortunately, when there are several balloons in the bunch, this method can result in a single tap popping multiple balloons. This is because the sprite images overlap (even though the physics bodies do not). There is also the undesired effect that a tap on the string will have the same effect as a tap on the balloon.
I have access to the coordinates of the touch point, but is it even possible to detect whether a point lies within the area defined by the physics body (as opposed to the node)?
Use the physics world's bodyAtPoint: method:
SKPhysicsBody* body = [self.scene.physicsWorld bodyAtPoint:location];
bodyAtPoint is buggy but you have a working alternative.
You can create rays using baloon polygon path.
[self.physicsWorld enumerateBodiesAlongRayStart:rayStart end:rayEnd
usingBlock:^(SKPhysicsBody *body, CGPoint point,CGVector normal, BOOL *stop) {
if(body.categoryBitMask & baloonCategory){
SKPhysicsBody *contactBody=body;
CGPoint contactPoint=point;
*stop=YES;
}
}];
I looked but wasn't able to find a way of checking whether a touch is made within the physicsBody of a sprite. I would suggest separating the string and the ballon sprites.
You can try any the following:
Using separate sprite nodes for the balloon and the string and connecting them using a SKPhysicsJoint.
Subclass SKSpriteNode and add two sprites, i.e. the balloon and the string as children of the Sprite. Then u can check whether a touch made on the sprite is on the ballon or not.
Additionally for the multiple ballon pop problem, you can modify your code like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSArray *nodes = [self nodesAtPoint:[touch locationInNode:self]];
//Instead of popping all nodes within the touch, pop only the first object.
if ([nodes count] > 0)
{
SKNode *node = [nodes firstObject];
[node removeFromParent];
SKAction *pop = [SKAction playSoundFileNamed:#"pop.wav" waitForCompletion:NO];
[self runAction:pop];
}
}

Cocos2d ccDrawLine performance issue

I use cocos2d 2.0 and Xcode 4.5. I am trying to learn how to draw a line. I can draw a line but after I drew few lines a serious performance issue occurs on Simulator.
Simulator starts to freeze, draws lines very very slowly and worst of all ,I guess because of -(void)draw is called every frame, the label on the screen becomes bold
before lines :
after lines;
I use following code :
.m
-(id) init
{
if( (self=[super init])) {
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Simple Line Demo" fontName:#"Marker Felt" fontSize:32];
label.position = ccp( 240, 300 );
[self addChild: label];
_naughtytoucharray =[[NSMutableArray alloc ] init];
self.isTouchEnabled = YES;
}
return self;
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
BOOL isTouching;
// determine if it's a touch you want, then return the result
return isTouching;
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [ touches anyObject];
CGPoint new_location = [touch locationInView: [touch view]];
new_location = [[CCDirector sharedDirector] convertToGL:new_location];
CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
// add my touches to the naughty touch array
[_naughtytoucharray addObject:NSStringFromCGPoint(new_location)];
[_naughtytoucharray addObject:NSStringFromCGPoint(oldTouchLocation)];
}
-(void)draw
{
[super draw];
ccDrawColor4F(1.0f, 0.0f, 0.0f, 100.0f);
for(int i = 0; i < [_naughtytoucharray count]; i+=2)
{
CGPoint start = CGPointFromString([_naughtytoucharray objectAtIndex:i]);
CGPoint end = CGPointFromString([_naughtytoucharray objectAtIndex:i+1]);
ccDrawLine(start, end);
}
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
ManageTraffic *line = [ManageTraffic node];
[self addChild: line z:99 tag:999];
}
I saw few Air Traffic Control games such as Flight Control, ATC Mania works really well.
Does this performance issue occur because of CCDrawLine/UITouch *touch or it is a common issue?
What Flight Control, ATC Mania might be using for line drawing?
Thanks in advance.
EDIT::::
OK I guess problem is not ccDrawLine, problem is I call ManageTraffic *line = [ManageTraffic node]; every time touch ends it calls init of node so it overrides scene
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
ManageTraffic *line = [ManageTraffic node];
[self addChild: line z:99 tag:999];
}
There's three things going on:
You assess performance on the Simulator. Test it on a device as Ben says.
You store points as strings and convert strings back to CGPoint. That is terribly inefficient.
ccDrawLine is not exactly efficient. For a couple dozen line segments it's ok. In your case maybe not (see below).
For #2, create a point class with only a CGPoint property and use that to store points in the array. Removes the string conversion or packing into NSData.
For #3 make sure that new points are only added if the new point is at least n points away from the previous point. For example a distance of 10 should reduce the number of points while still allowing for relatively fine line details.
Also regarding #3, I notice you add both current and previous point to the array. Why? You only need to add the new point, and then draw points from index 0 to 1, from 1 to 2, and so on. You only have to test for the case where there is only 1 point. The previous touch event's location is always the next touch event's previousLocation. So you're storing twice as many points as you need to.

rotate an image with touch

I making an app which sets the sleep timer with a clock .Basically it is clock which has a single hand which user can move to set his sleep time .I tried to rotate the image with uitouch but it rotates from middle but i want it to rotate from the tip.Secondly i want that the image only rotates when user is touching the tip of the image but in my project the image is also rotating when use touches any part of the screen.Also i want to rotate the image in both directions but in my project it moves only clockwise due to this method
image.transform = CGAffineTransformRotate(image.transform, degreesToRadians(1));
Can anybody give me hints or solutions about how can it be done?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//
touch = [[event allTouches] anyObject];
touchLocation = [touch locationInView:touch.view];
NSLog(#"began");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// get touch event
[image.layer setAnchorPoint:CGPointMake(0.0,0.0)];
if ([touch view] == image) {
image.transform = CGAffineTransformRotate(image.transform, degreesToRadians(1));
//image.center = touchLocation;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"end");
}
In detail, you can custom a rotateView,then:
1: In the delegate method of "touchesBegan", get initialPoint of finger and initialAngle.
2: During "touchesMoved",get the newPoint of finger:
CGPoint newPoint = [[touches anyObject] locationInView:self];
[self pushTouchPoint:thePoint date:[NSDate date]];
double angleDif = [self angleForPoint:newPoint] - [self angleForPoint:initialPoint];
self.angle = initialAngle + angleDif;
[[imageView layer] setTransform:CATransform3DMakeRotation(angle, 0, 0, 1)];
3: At last, in "touchesEnded" you can calculate final AngularVelocity.
If anything being confused, for more detail, you can write back.
To rotate it the other way you just use:
image.transform = CGAffineTransformRotate(image.transform, degreesToRadians(1));
And to rotate form the tip you should use setAnchorPoint (like you used in your code, but i think you should do: [image setAnchorPoint]).
more on the subject: setAnchorPoint for UIImage?
I tried to rotate the image with uitouch but it rotates from middle but i want it to rotate from the tip
I dont know any way in SDK to rotate an element on its extreme end. If you want to have that effect take the clock handle image you have twice in length with half portion transparent. It is not a direct solution, but a workaround.
Also i want to rotate the image in both directions but in my project it moves only clockwise due to this method
As per iOS developer documentation, a positive angle value specifies counterclockwise rotation and a negative value specifies clockwise rotation.

Resources