Lock sprite center to tile centers of tilemap - ios

I want to be able to drag a sprite across a tilemap but am having trouble making the sprite lock to the tiles. First I had:
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchLocation = [touch locationInView:touch.view];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
objectInTouch.position=touchlocation;
}
That didn't work because it uses the screen coordinates instead of the tilemap's?
Then I figured that each tile is 16x16 px so I would make a "border" around the sprite (objectInTouch) and depending on if it hit the top, bottom, left, or right, the sprite would move accordingly. Here's what I tried but I get "Expression is not assignable."
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchLocation = [touch locationInView:touch.view];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
int xborder = objectInTouch.position.x+16;
int yborder = objectInTouch.position.y+16;
int xleftborder = objectInTouch.position.x-16;
int ybottomborder = objectInTouch.position.y-16;
if(touchLocation.x >= xborder)
objectInTouch.position.x = objectInTouch.position.x+16; -> Expression not assignable
if(touchLocation.y >= yborder)
objectInTouch.position.y = objectInTouch.position.y+16; -> Expression not assignable
if(touchLocation.x <= xleftborder)
objectInTouch.position.x = objectInTouch.position.x-6;; -> Expression not assignable
if(touchLocation.y <= ybottomborder)
objectInTouch.position.y = objectInTouch.position.y-16; -> Expression not assignable
}
Oh and if anyone could refer me to some good books/documents on this sort of stuff that'd be great!
Thanks!

Related

Sprite Kit: How to make a node "jump" and also move it left or right, dependant on node position and length of touch?

This is my first post so go easy haha.
I'm new to 'iOS' 'coding', 'Xcode' and 'spritekit'. I'm looking to make an image node "jump" a distance on the positive y-axis if I touch anywhere on the screen, although if I touch somewhere to the left or right if the image and hold for a certain time, it moves in the respective left or right direction, a distance respective to the length of the touch.
Not sure if that's very clear, but any help would be appreciated! Thanks!
You could move a node like this
In touchesEnded: or touchesBegan: method:
{
node.position.y += 50;
}
In order for sprite to move somewhere you could also use actions, there is a family of actions like moveTo action.
I would suggest picking upa tutorial or a book about sprite kit. Good site with free tutorials is raywenderlich.com
Please Try this code :
a sprite move : left/right according to your touch direction anywhere on screen
// CGPoint initialPos; // in .h file
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchPt = [touch locationInView:touch.view];
initialPos = [[CCDirector sharedDirector] convertToGL:touchPt];
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
CGPoint currentPos=[myTouch locationInView:[myTouch view]];
currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];
float diffX = currentPos.x - initialPos.x;
float diffY = currentPos.y - initialPos.y;
CGPoint velocity = ccp(diffX, diffY);
initialPos = currentPos;
[Sprite setPosition:ccpAdd([SmileBall position], velocity)];
}
- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch=[touches anyObject];
CGPoint currentPos=[myTouch locationInView:[myTouch view]];
currentPos=[[CCDirector sharedDirector] convertToGL:currentPos];
float diffX = currentPos.x - initialPos.x;
float diffY = currentPos.y - initialPos.y;
CGPoint velocity = ccp(diffX, diffY);
initialPos = currentPos;
[Sprite setPosition:ccpAdd([SmileBall position], velocity)];
}
a sprite jump up: according to your touch on screen
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CCSprite *RunningChar;
RunningChar=(CCSprite *)[self getChildByTag:10];
UITouch *touch = [touches anyObject];
CGPoint touchPt = [touch locationInView:touch.view];
touchPt = [[CCDirector sharedDirector] convertToGL:touchPt];
float radian = ccpToAngle(ccpSub(touchPt, prevPoint));
float degrees = CC_RADIANS_TO_DEGREES(radian);
if (degrees >= 22.5 && degrees <= 112.5) // for upward direction :). otherwise you can write only code without ant condition
{
CCJumpTo *jump=[CCJumpTo actionWithDuration:0.7 position:CGPointMake(RunningChar.position.x, 87) height:110 jumps:1];
[RunningChar runAction:seq];
}
else
{
}
}
Try this :)

How to group sprites in cocos2d for touch detection?

I use this code example to detect whether sprite is touched.
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedlocation = [[CCDirector sharedDirector] convertToGL: location];
CGPoint convertedNodeSpacePoint = [self convertToNodeSpace:convertedlocation];
if (CGRectContainsPoint([_sprite boundingBox],convertedNodeSpacePoint))
{}
I have many sprites, and I must copy that code many times for each of them just changing sprite names. How can I make this process a bit shorter ? Making a NSArray, maybe?
If I have get your question right, you can use for loop and check all children. Something like this-
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
for (CCSprite *spr in self.children) {
if ([spr isKindOfClass:[CCSprite class]]) {
float distance = pow(spr.position.x - location.x, 2) + pow(spr.position.y - location.y, 2);
distance = sqrt(distance);
if (distance1 <= 150) {
//do something with your sprite
}
}
}

Dragging a sprite with cocos2d produces a lag

I have the following piece of code to drag a sprite around the screen using cocos2D:
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
CGPoint newPos = ccpAdd([mySprite position], translation);
[mySprite setPosition:newPos];
}
The sprite is shown on the screen and I can drag it around the screen with my finger, but the sprite is slower than my finger lagging behind about a quarter of an inch.
What can I do to avoid this lag?
Thanks,
GA

CCSprite rotation like Car Steering in Cocos2D

Is there any way to rotate a CCSprite to behave like a car Steering?If i release my touch with the CCSprite,it should automatically return to its original position in a smooth way exactly like a steering!Im using the below code,
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
//acquire the previous touch location
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
location = [touch locationInView:[touch view]];
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, Steering.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, Steering.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
dialrotation+=currentTouch-PreviousTouch;
sprite.rotation=dialrotation;
}
This rotates my CCSprite Smoothly,but i don't know how to return back to its original position like Car Steering.Is there any way to do so in Cocos2D?
In ccTouchesBegan method store the value of sprite.rotation somewhere, in ccTouchesEnded run CCRotateTo action on your sprite using this stored value.

How can i drag a CCSprite with velocity/speed in cocos2d by using CCTouchMoved?

I have an sprite, i want to move it by using my finger to move around the screen ~ drag.
I want my sprite move with velocity, it means do not as fast as my finger move.
it seem like this video: http://www.youtube.com/watch?v=Vair3CIxZEw (from 0:12 to 0:53)
Here is my ccTouch code. How can i fix to make it move look smoother?
Thank you!!! :)
simply return TRUE
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
return TRUE;
}
and
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
if (CGRectContainsPoint(_car.boundingBox, touchLocation)) {
CGPoint newPos = ccpAdd(_car.position, translation);
_car.position = newPos;
}
}
Try using CCMoveTo action for smooth moving
CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
if (CGRectContainsPoint(_car.boundingBox, touchLocation)) {
CGPoint newPos = ccpAdd(_car.position, translation);
id moveAction = [CCMoveTo actionWithDuration:0.5f position:newPos];
[_car runAction:moveAction];
}

Resources