2013-03-27 25 views
0

Here is the source用cocos2d定位

問題是我的射彈的開始和結束位置都沒有了。我有NSLog'd所有的CGPoints和我所看到的被輸入CCSequence的點是正確的。但從我在屏幕上看到的不同。

我認爲最新錯誤的一個重要線索是,如果創建一個新的CCSprite並像使用它一樣使用彈道,那麼這個路徑就起作用。

這裏是遊戲水平層上的addProjectile方法(GameLevels:Level1Layer)

-(void)addProjectile:(Projectiles*)projectile 
{ 
    NSLog(@"projectile position %@",NSStringFromCGPoint(projectile.position)); 
    NSLog(@"wizard position %@",NSStringFromCGPoint(self.wizardHero.position)); 
    NSLog(@"wizard position worldspace %@",NSStringFromCGPoint([self convertToWorldSpace:self.wizardHero.position])); 
    NSLog(@"destination position %@",NSStringFromCGPoint(projectile.destination.position)); 

    [self addChild:projectile]; 

    // Determine where we wish to shoot the projectile to 
    int realX; 

    CGPoint diff = ccpSub(projectile.destination.position,projectile.position); 
    if (diff.x > 0) 
    { 
     realX = (self.tileMap.mapSize.width * self.tileMap.tileSize.width) + 
     (projectile.contentSize.width/2); 
    } else { 
     realX = -(self.tileMap.mapSize.width * self.tileMap.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); 

    // Determine the length of how far we're shooting 
    int offRealX = realX - projectile.position.x; 
    int offRealY = realY - projectile.position.y; 
    float length = sqrtf((offRealX*offRealX) + (offRealY*offRealY)); 
    float velocity = 280/1; // 280pixels/1sec 
    float realMoveDuration = length/velocity; 

    // Move projectile to actual endpoint 
    id actionMoveDone = [CCCallFuncN actionWithTarget:self 
              selector:@selector(projectileMoveFinished:)]; 
    [projectile runAction: 
    [CCSequence actionOne: 
     [CCMoveTo actionWithDuration: realMoveDuration 
          position: realDest] 
         two: actionMoveDone]]; 

    [self.projectiles addObject:projectile]; 
} 

和輸出從那些NSLog語句將是(供參考:正在從self.wizardHero加入彈丸,因此這是它的開始位置;目的地將是self.redEnemy):

2013年3月27日10:41:57.295 GridWars [13025:C07]彈丸位置{105, 178}

2013年3月27日10:41:57.296 GridWars [13025:C07]嚮導位置{105,178}

2013年3月27日10:41:57.297 GridWars [13025:C07]嚮導位置世界空間 {105,178}

2013年3月27日10:41:57.298 GridWars [13025:C07]目的地位置{299, 174}

2013年3月27日11:34:46.608 GridWars [13344 :c07] realDest {650, 166}

2013-03-27 11:34:46.608 GridWars [13344:c07] length 545.1320 80

2013年3月27日11:34:46.610 GridWars [13344:C07] realMoveDuration 1.946900

我認識到,與超類我有它有點分不清什麼是繼續下去(有點爲什麼我包括來源)。基本上,雖然我有從CCNode

Projectiles : CCNode 
     sprite : CCSprite 
     destination : GameCharacters 
     damage : int 


GameLevels : CCNode 
     hud : HudLayer 
     tileMap : CCTMXTiledMap 
     enemies : NSMutablArray 
     projectiles : NSMutablArray 

GameCharacters : CCNode 
     hp : int 
     juice : int 
     sprite : CCSprite 
     selectedTargets : NSMutablArray 
     hostLayer : GameLevels 

子類三大超類那麼這三個類實際實現這些

 BasicProjectile : Projectiles 
     Level1Layer : GameLevels 
     WizardHero : GameCharacter 
     RedEnemy : GameCharacter 

雖然問題有所發展我已經發布這對cocos2d forums並獲得一些幫助,讓我到了這一點(看到拋射物,但路徑是錯的,而不是看到它們)。但它已經有幾天了,希望有更多的幫助。

GameProjectiles:CCNode

#import "CCNode.h" 

//forward declaration because we just need to hold a reference 
@class GameCharacters; 

@interface GameProjectiles : CCNode 

@property(nonatomic, strong) CCSprite * sprite; 
@property(nonatomic,strong) GameCharacters * destination; 
@property(nonatomic) int damage; 
-(id)initWithDestination:(GameCharacters*)Destination; 
-(CGSize)contentSize; 

@end 


#import "GameProjectiles.h" 

@implementation GameProjectiles 

-(id)initWithDestination:(GameCharacters*)Destination 
{ 

    if (self = [super init]) { 
     _damage = 0; 
     _destination = Destination; 
    } 
    return self; 
} 

-(CGSize)contentSize{ 
    return self.sprite.contentSize; 
} 

-(void)setPosition:(CGPoint)position 
{ 
    [super setPosition:position]; 
    self.sprite.position = position; 
} 
@end 

BasicProjectile

#import "GameProjectiles.h" 

@interface BasicProjectile : GameProjectiles 

@end 


#import "BasicProjectile.h" 

@implementation BasicProjectile 

-(id)initWithDestination:(GameCharacters*)Destination 
{ 
    if (self = [super init]) { 
     self.damage = 25; 
     self.sprite = [CCSprite spriteWithFile:@"Projectile.png"]; 
     [self addChild:self.sprite z:1000 ]; 
     self.destination = Destination; 
    } 
    return self; 
} 

@end 
+0

你在屏幕上看到什麼?日誌readDest,長度和realMoveDuration PLZ。 – YvesLeBorg 2013-03-27 17:26:44

+0

射彈遠北(頂部),它可能是正確的太多,但很難看到它是如此北方。我用這些值更新上面的代碼,一秒 – 2013-03-27 17:31:08

+0

好吧,如果你能看到嚮導,而不是彈丸,它們都有相同的位置!我只能假設他們的'位置'是相對於不同的東西(有不同的父母)......這也可能與目標對象不同:)。 – YvesLeBorg 2013-03-27 18:06:50

回答

1

因爲你彈和目標是一個獨立的CCNode裏面,他們的邊框返回自己的位置其父內,而不是在實際屏幕座標(你想要的情況)。

要你能做的POS轉換這樣

CGPoint targetPos = [basicProjectile.destination.sprite.parent convertToWorldSpace:basicProjectile.destination.sprite.position]; 
    NSLog(@"targetPos %@",NSStringFromCGPoint(targetPos)); 
    CGRect targetRect = [self boundingRectForSprite:basicProjectile.destination.sprite andPos:targetPos]; 

    CGPoint projectilePos = [basicProjectile.sprite.parent convertToWorldSpace:basicProjectile.sprite.position]; 
    NSLog(@"projectilePos %@",NSStringFromCGPoint(projectilePos)); 
    CGRect projectileRect = [self boundingRectForSprite:basicProjectile.destination.sprite andPos:projectilePos]; 

boundingRectForSprite方法使使用精靈邊框和位置,然後計算最終RECT服用(0.5,0.5)錨點。

現在targetRectprojectileRect是實際的屏幕座標。