Whats Wrong With This? - Cocos2D - Touches
Im trying to implement "shooting" on a project and everything seems to be
working fine except.... Here's the deal.
I Press the screen, you get the CGPoint of touch Location, that location
is sent to a layer where you process where the position where the touch is
and the origin from where you're shooting. More on the tutorial I took to
make this happen Everything seems correct except it ain't shooting to
where Im clicking...
I would be most greatful if someone help me spot what Im doing wrong!
int realX;
// Direction to where we're shooting
CGPoint diff = ccpSub(location, player.position);
if (diff.x > 0)
{
realX = (tiledMap.mapSize.width * tiledMap.tileSize.width) +
(projectile.contentSize.width/2);
} else {
realX = -(tiledMap.mapSize.width * tiledMap.tileSize.width) -
(projectile.contentSize.width/2);
}
float ratio = (float) diff.y / (float) diff.x;
int realY = ((realX - projectile.position.x) * ratio) +
projectile.position.y;
CGPoint realDest = ccp(realX, realY);
CCLOG(@"Touch:(%f,%f)",location.x,location.y);
CCLOG(@"Player:(%f,%f)",player.position.x,player.position.y);
CCLOG(@"diff:(%f,%f)",diff.x,diff.y);
CCLOG(@"realX:%d",realX);
CCLOG(@"realY:%d",realY);
CCLOG(@"realDest = (%f,%f)",realDest.x,realDest.y);
I made several Logs to check if everything is right, Touch seems right,
playerPosition seems correct...but everything else is weird.... look at
this, here's a couple of the logs I made.
Touch:(296.000000,94.000000)
Player:(400.000000,464.000000)
diff:(-104.000000,-370.000000)
realX:-810
realY:-3840
realDest = (-810.000000,-3840.000000)
Touch at around ~45degrees(x+,y+) from the player sprite.
Touch:(355.000000,275.000000)
Player:(400.000000,464.000000)
diff:(-45.000000,-189.000000)
realX:-810
realY:-4618
realDest = (-810.000000,-4618.000000)
Touched at aroun ~45degs(x-,y-) from sprite
Touch:(81.000000,63.000000)
Player:(400.000000,464.000000)
diff:(-319.000000,-401.000000)
realX:-810
realY:-1057
realDest = (-810.000000,-1057.000000)
What am I missing or what am I doing wrong?
Please help me! :(
Thank you fro your time!
No comments:
Post a Comment