i 试图以下述方式检测到Collision 两个sprites......但当我试图驾车时,没有发生碰撞。
- (void)update:(ccTime)dt {
CGRect projectileRect = CGRectMake(projectile.position.x - (projectile.contentSize.width/2),
projectile.position.y - (projectile.contentSize.height/2),
projectile.contentSize.width,
projectile.contentSize.height);
//CGRectMake(0,220,320,50);
CGRect targetRects = CGRectMake(_monkey.position.x - (_monkey.contentSize.width/2),
_monkey.position.y - (_monkey.contentSize.height/2),
_monkey.contentSize.width,
_monkey.contentSize.height);
if (CGRectIntersectsRect(projectileRect, targetRects)) {
NSLog(@"ha ha Collision detected");
iii
iii
<>strong>projectilespirite is animating from summary to the past and key,spirite is animating from留 to right at the Worst the projectile go through the monkey but the log do not have been calls?
- (void)update:(ccTime)dt {
CGRect projectileRect = [projectile boundingBox];
CGRect targetRects = [_monkey boundingBox];
if (CGRectIntersectsRect(projectileRect, targetRects))
{
NSLog(@"ha ha Collision detected");
iii
CGRect projectileRects = CGRectMake(projectile.position.x - (projectile.contentSize.width/2),
projectile.position.y - (projectile.contentSize.height/2),
projectile.contentSize.width,
projectile.contentSize.height);
CGRect targetRect = CGRectMake(_monkey.position.x - (_monkey.contentSize.width/2),
_monkey.position.y - (_monkey.contentSize.height/2),
_monkey.contentSize.width,
_monkey.contentSize.height);
if (CGRectIntersectsRect(projectileRects, targetRect)) {
NSLog(@"@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
iii
iii
- 废除 MoveFinished (id)sender{
//NSLog(@"spriteMoveFinished");
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];
if (sprite.tag == 1) {
[_targets removeObject:sprite];
iii else if (sprite.tag == 2) {
[_projectiles removeObject:sprite];
iii
iii
- (避免) 具体目标1
projectile = [CCSprite spriteWithFile:@"egg.png" rect:CGRectMake(0, 0, 10, 10)];
projectile.position = ccp(_bear.position.x,_bear.position.y-20);
projectile.tag=2;
[self addChild:projectile];
CGPoint realDest = ccp(_bear.position.x, _bear.position.y - 380);
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Move projectile to actual endpoint
[projectile runAction:[CCSequence actions:
[CCMoveTo actionWithDuration:actualDuration position:realDest],
[CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
nil]];
// Add to projectiles array
projectile.tag = 2;
[_projectiles addObject:projectile];
iii
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
iii
- (BOOL) 活动:活动*
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation))
{
if (![_walkMonkey isDone]) {
[_monkey runAction:_walkMonkey];
iii
iii
else {
iii
return YES;
iii
- 加入: 活动:活动*
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation))
{
[_monkey stopAction:_walkMonkey];
iii
iii
- (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 (translation.x > 3) {
_monkey.flipX=YES;
iii
else if (translation.x < -3){
_monkey.flipX=NO;
iii
if(CGRectContainsPoint(CGRectMake(40,0,240,50),touchLocation))
{
CGPoint newPos = ccpAdd(translation,_monkey.position);
if(newPos.x >= 320 || newPos.x <= 20)
{
NSLog(@"monkey not walking");
iii
else {
newPos.y = 100;
_monkey.position = newPos;
iii
iii
iii