Moving objects in an array using UITouch SpriteKit - nsmutablearray

I'm trying to move SKSpritenode in an array like a scrollbar where all objects move together according to the move location:
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
NSEnumerator *e = [_blockStream objectEnumerator];
PBAIceBlock *block;
while (block = [e nextObject])
{
//this says the expresion is not assignable
block.position.y += location.y;
}
}
Where the comments says, I get an error, could some one please point me in the right direction on how to implement something like this?
Using Sprite kit in xcode 5?

A solution is to create a CGPoint to store a value. set that value in touchesBegan, then:
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
int yOFFSET = location.y - _touchLocation.y ;
NSLog(#"yOFFSET : %d", yOFFSET);
NSEnumerator *e = [_blockStream objectEnumerator];
PBAIceBlock *block;
while (block = [e nextObject]) {
CGPoint _center = block.position;
_center.y += yOFFSET;
block.position = _center;
}
_touchLocation = location;
}
This problem happened because you cannot call the position.y specifically.

Related

How to get touched node center position on Sprite Kit?

I am using Sprite Kit in Xcode and having problem with position.
I want to drag the node and drop to target area.If not the right target than send the node back to old position.
My code is working but when I touch node (except the center of node) anywhere will be holding center point of old position.So if target is wrong the node going back as little moved because of touched point was different than center of node.
Therefore If I can get center position of touched node than I can send back with absolute position!
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
[self selectNodeForTouch:positionInScene];}
Than
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGPoint previousPosition = [touch previousLocationInNode:self];
CGPoint translation = CGPointMake(positionInScene.x - previousPosition.x, positionInScene.y - previousPosition.y);
[self panForTranslation:translation];
}
//
-(void)selectNodeForTouch:(CGPoint)touchLocation{
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint: touchLocation];
if (![_selectedNode isEqual:touchedNode.name]) {
_selectedNode =touchedNode;
if ((newPositionStone.x-28)<5 || (newPositionStone.x-28)>(5*(-1))){
NSLog(#"Great! you drop node to target ");
}
else{
NSLog(#"Didn't drop node to target");//Send back to old position
_selectedNode.position = oldPositionStone;
}
}}
Thanks in advance!
You need to store the object's position at the moment of touch so you can revert back to it if your drag destination fails.
Create a CGPoint variable in your #implementation like so:
#implementation MyScene
{
CGPoint objectStartPosition;
}
In the touchesBegan: set the object's current position:
objectStartPosition = newPositionStone.position;
Then in your touchesEnded: check if the node's destination is valid:
if(whatever x and y positions you need to check for in here)
{
// do whatever you need to
} else {
newPositionStone.position = objectStartPosition;
}
The only solution I founded fixing the node on specific position.
But wondering does Sprite Kit has method to find position of touched node?
Here is my solution
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
if (positionInScene.x>400.0 && positionInScene.x<430.0) {
//Fix the position to original
oldPositionStone.x = 414;
oldPositionStone.y = 25;
}
[self selectNodeForTouch:positionInScene];}
//Than
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
NSLog(#"Touches END %f %f",positionInScene.x,positionInScene.y);
if (positionInScene.x>15 && positionInScene.x<40) {
newPositionStone.x=28;
newPositionStone.y = 25;
if ([_selectedNode.name isEqualToString:#"stone"]) {
_selectedNode.position=newPositionStone;
}
}
else{
positionInScene = oldPositionStone;
_selectedNode.position = positionInScene;
}
[self selectNodeForTouch:positionInScene];}

How do you programmatically position the mkmapregion to fit annotation

I'm trying to figure out how to reposition the MKMap region programmatically so that my annotation (automatically selected when the map loads) will all fit centered.
Current Result: https://www.evernote.com/shard/s46/sh/7c7d2ed8-203c-4878-af8c-83ff77ad7b21/ce7786acdf66b0782fc689b72d1b67e7
Desired Result: https://www.evernote.com/shard/s46/sh/21fb0eab-d5c4-4e6d-b05b-322e7dcce8ab/ab816f2a24f11b9c9e15bf55ac648f72
I have tried to reposition everything in - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views but that didn't work. Is there a better approach?
// here is viewWillAppear logic
[self.mapView removeAnnotations:self.mapView.annotations];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLGeocodeCompletionHandler completionHandler = ^(NSArray *placemarks, NSError *error) {
if (error) {
EPBLog(#"error finding placemarks: %#", [error localizedDescription]);
} else {
if (placemarks) {
[placemarks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
CLPlacemark *placemark = (CLPlacemark *)obj;
if ([placemark.country isEqualToString:#"United States"]) {
EPBAnnotation *annotation = [EPBAnnotation annotationWithCoordinate:placemark.location.coordinate];
annotation.title = self.locationObj.locationName;
annotation.subtitle = self.locationObj.locationAddress;
[self.mapView addAnnotation:annotation];
self.mapView.selectedAnnotations = #[annotation];
[self.mapView setCenterCoordinate:placemark.location.coordinate];
/**
* #todo
* MOVE THIS OUTTA HERE
*/
MKCoordinateRegion region = {{0.0f, 0.0f}, {0.0f, 0.0f}};
region.center = placemark.location.coordinate;
region.span.longitudeDelta = 0.003f;
region.span.latitudeDelta = 0.003f;
[self.mapView setRegion:region animated:YES];
[self.mapView regionThatFits:region];
*stop = YES;
}
}];
}
}
};
[geocoder geocodeAddressString:self.locationObj.locationAddress completionHandler:completionHandler];
Following method will fit the map on region to show all annotations. You can call this method in Map's didAddAnnotations method.
- (void)zoomToFitMapAnnotations {
if ([mMapView.annotations count] == 0) return;
int i = 0;
MKMapPoint points[[mMapView.annotations count]];
//build array of annotation points
for (id<MKAnnotation> annotation in [mMapView annotations]){
points[i++] = MKMapPointForCoordinate(annotation.coordinate);
}
MKPolygon *poly = [MKPolygon polygonWithPoints:points count:i];
[mMapView setRegion:MKCoordinateRegionForMapRect([poly boundingMapRect]) animated:YES];
}
Howevcer you should see if you want to add user location annotation in visible area also. If you don't then in loop check if current annotation is MkUserLocation and don't add it's points in points array.
if ([annotation isKindOfClass:[MKUserLocation class]]) {
continue:
}
Now if you wanted an Annotation to be in center and selected automatically then do this
annotation.coordinate=mMapView.centerCoordinate;
[mMapView selectAnnotation:annotation animated:YES];

Pinch gesture in cocos2d, how to?

I need to implement a simple pinch gesture in my cocos2d project with two or more sprites. Would you be so kind to tell me, how can I handle pitch gesture?
I need something like this:
-(void)handlePinchGesture:(UIPinchGestureRecognizer *)recognizer
{
if (recognizer.state != UIGestureRecognizerStateCancelled)
{
if (recognizer.numberOfTouches == 3)
{
CGPoint firstPoint = [recognizer locationOfTouch:0 inView:recognizer.view];
CGPoint secondPoint = [recognizer locationOfTouch:1 inView:recognizer.view];
CGPoint thirdPoint = [recognizer locationOfTouch:2 inView:recognizer.view];
CGPoint fourthPoint = [recognizer locationOfTouch:3 inView:recognizer.view];
ball1.position=firstPoint;
ball2.position=secondPoint;
ball3.position=thirdPoint;
ball4.position=fourthPoint;
}
}
}
Here what I did (it took me quite a lot of googling)
In implementation file
-(id)init
{
self = [super init];
self.isTouchEnabled=YES;
if (self != nil)
{
}
return self;
}
//pinch recognising
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allUserTouches=[event allTouches];
if(allUserTouches.count==2)
{
UITouch* touch1=[[allUserTouches allObjects] objectAtIndex:0];
UITouch* touch2=[[allUserTouches allObjects] objectAtIndex:1];
CGPoint touch1location=[touch1 locationInView:[touch1 view]];
CGPoint touch2location=[touch2 locationInView:[touch2 view]];
touch1location=[[CCDirector sharedDirector] convertToGL:touch1location];
touch2location=[[CCDirector sharedDirector] convertToGL:touch2location];
ball.position=touch1location;
newBall.position=touch2location;
float currentdist=ccpDistance(touch1location, touch2location);
oldDist=currentdist;
}
}
-(void)ccTouchesMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
NSSet *allUserTouches=[event allTouches];
if(allUserTouches.count==2)
{
UITouch* touch1=[[allUserTouches allObjects] objectAtIndex:0];
UITouch* touch2=[[allUserTouches allObjects] objectAtIndex:1];
CGPoint touch1location=[touch1 locationInView:[touch1 view]];
CGPoint touch2location=[touch2 locationInView:[touch2 view]];
touch1location=[[CCDirector sharedDirector] convertToGL:touch1location];
touch2location=[[CCDirector sharedDirector] convertToGL:touch2location];
float currentdist=ccpDistance(touch1location, touch2location);
if (oldDist>=currentdist)
{
//[spriteToZoom setScale:spriteToZoom.scale-fabs((oldDist-currentdist)/100)];
[ball setPosition:touch1location];
[newBall setPosition:touch2location];
NSLog(#"pinch out");
}
else
{
//[spriteToZoom setScale:spriteToZoom.scale+fabs((oldDist-currentdist)/100)];
[ball setPosition:touch1location];
[newBall setPosition:touch2location];
NSLog(#"pinch in");
}
}
}

Drawing more than one line/path on iPhone using Array of points

I'm trying to create a drawing view by drawing the line(s) using an Array of CGPoints.
I'm currently able to draw more than one line but the problem is that I don't know how to break each line when touch is ended.
The current status is -
line1 is drawn until touchended
When touchbegan again, line2 is drawn as well, BUT, line1 endpoint is connected with line2 starting point.
Implementation as follows:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSUInteger tapCount = [[touches anyObject] tapCount];
if (tapCount == 2)
{
[pointArray removeAllObjects];
[self setNeedsDisplay];
}
else
{
if ([pointArray count] == 0)
pointArray = [[NSMutableArray alloc] init];
UITouch *touch = [touches anyObject];
start_location = [touch locationInView:self];
[pointArray addObject:[NSValue valueWithCGPoint:start_location]];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
current_location = [touch locationInView:self];
[pointArray addObject:[NSValue valueWithCGPoint:current_location]];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)drawRect:(CGRect)rect
{
if ([pointArray count] > 0)
{
int i;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
for (i=0; i < ([pointArray count] -1); i++)
{
CGPoint p1 = [[pointArray objectAtIndex:i]CGPointValue];
CGPoint p2 = [[pointArray objectAtIndex:i+1]CGPointValue];
CGContextMoveToPoint(context, p1.x, p1.y);
CGContextAddLineToPoint(context, p2.x, p2.y);
CGContextStrokePath(context);
}
}
}
Please advise :-))
Thank you in advance,
Dudi Shani-Gabay
In this case, I think its better for you to keep separate arrays for separate lines. Let the "pointArray" be an array having number of arrays for each line drawn.
In "touchesBegan" method, you need to add new array object to pointArray as follows:
start_location = [touch locationInView:self];
NSMutableArray *newLineArray = [[NSMutableArray alloc] init];
[pointArray addObject:newLineArray];
[[pointArray lastObject] addObject:[NSValue valueWithCGPoint:start_location]];
In "touchesMoved", you have to replace
[pointArray addObject:[NSValue valueWithCGPoint:current_location]];
with the following:
[[pointArray lastObject] addObject:[NSValue valueWithCGPoint:current_location]];
In the "drawRect" method, the 'for' loop should be like this:
for (i=0; i < [pointArray count]; i++)
{
for (int j=0; j < ([[pointArray objectAtIndex:i] count] -1); j++)
{
CGPoint p1 = [[[pointArray objectAtIndex:i] objectAtIndex:j]CGPointValue];
CGPoint p2 = [[[pointArray objectAtIndex:i] objectAtIndex:j+1]CGPointValue];
CGContextMoveToPoint(context, p1.x, p1.y);
CGContextAddLineToPoint(context, p2.x, p2.y);
CGContextStrokePath(context);
}
}

cocos2d box2d scale sprite -> removechild (how?)

I am little slow in English, please do understand.
Here is my source code:
- (void)createBall:(CGPoint)touchedAt{
CGSize winSize = [CCDirector sharedDirector].winSize;
ball2 = [CCSprite spriteWithFile:#"Ball.png" rect:CGRectMake(0, 0, 54, 54)];
ball2.position = ccp(touchedAt.x,touchedAt.y);
[self addChild:ball2];
b2BodyDef ballBodyDef2;
ballBodyDef2.type = b2_dynamicBody;
ballBodyDef2.position.Set(touchedAt.x/PTM_RATIO, touchedAt.y/PTM_RATIO);
ballBodyDef2.userData = ball2;
b2Body *body2 = _world->CreateBody(&ballBodyDef2);
b2CircleShape circle;
circle.m_radius = 89.0/PTM_RATIO;//(arc4random()*26.0)/PTM_RATIO;
b2FixtureDef ballShapeDef2;
ballShapeDef2.shape = &circle;
ballShapeDef2.density = 1.0f;
ballShapeDef2.friction = 0.2f;
ballShapeDef2.restitution = 0.8f;
body2->CreateFixture(&ballShapeDef2);
}
-(void)createBall2
{
CGSize winSize = [CCDirector sharedDirector].winSize;
globalSprite = [CCSprite spriteWithFile:#"Ball.png"];
globalSprite.position = ccp(winSize.width/2 + globalSprite.contentSize.width, winSize.height/2);
[self addChild:globalSprite];
b2BodyDef ballBodyDef3;
ballBodyDef3.type = b2_dynamicBody;
ballBodyDef3.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
ballBodyDef3.userData = globalSprite ;
b2Body *body3 = _world->CreateBody(&ballBodyDef3);
b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;//(arc4random()*26.0)/PTM_RATIO;
b2FixtureDef ballShapeDef3;
ballShapeDef3.shape = &circle;
ballShapeDef3.density = 1.0f;
ballShapeDef3.friction = 0.2f;
ballShapeDef3.restitution = 0.8f;
body3->CreateFixture(&ballShapeDef3);
}
// initialize your instance here
-(id) init
{
if( (self=[super init])) {
// enable touch
// enable accelerometer
CGSize winSize = [CCDirector sharedDirector].winSize;
self.isAccelerometerEnabled = YES;
self.isTouchEnabled = YES;
// Create sprite and add it to the layer
// Create a world
b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
bool doSleep = true;
_world = new b2World(gravity, doSleep);
// Create edges around the entire screen
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body *groundBody = _world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundBox;
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
// Create ball body and shape
[self schedule:#selector(tick:)];
//[self schedule:#selector(gameLogic:) interval:1.0];
[self createBall2];
}
return self;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// Choose one of the touches to work with
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
[self createBall:location];
}
- (void)tick:(ccTime) dt {
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *ballData = (CCSprite *)b->GetUserData();
ballData.position = ccp(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
I want to
touch -> sprite create(circle) -> sprite scale -> sprite remove
but
- (void)tick:(ccTime) dt <---------- this is simulator turn off!
I want to way
Try this :
world->DestroyBody(sprite);

Resources