2011-11-26 47 views
-3

我有SneakyJoystick啓動並運行,但我想要移動我的精靈瓷磚的瓷磚在一定的頻率...我對iPhone編程和cocos2d很新,所以我不完全知道整個SneakyJoystick thingy是如何工作的。 我從一本書的代碼,並再次:我只是希望我的精靈一樣在遊戲口袋妖怪瓦琉璃磚移動...SneakyJoystick - 由某些點/瓷磚的運動

-(void)initJoystickAndButtons { 
    CGSize screenSize = [CCDirector sharedDirector].winSize; 
    CGRect joystickBaseDimensions = 
    CGRectMake(0, 0, 128.0f, 128.0f); 
    CGRect jumpButtonDimensions = 
    CGRectMake(0, 0, 64.0f, 64.0f); 
    CGRect attackButtonDimensions = 
    CGRectMake(0, 0, 64.0f, 64.0f); 
    CGPoint joystickBasePosition; 
    CGPoint jumpButtonPosition; 
    CGPoint attackButtonPosition; 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
     // The device is an iPad running iPhone 3.2 or later. 
     CCLOG(@"Positioning Joystick and Buttons for iPad"); 
     joystickBasePosition = ccp(screenSize.width*0.0625f,screenSize.height*0.052f); 
     jumpButtonPosition = ccp(screenSize.width*0.946f,screenSize.height*0.052f); 
     attackButtonPosition = ccp(screenSize.width*0.947f,screenSize.height*0.169f); 
    } else { 
     // The device is an iPhone or iPod touch. 
     CCLOG(@"Positioning Joystick and Buttons for iPhone"); 
     joystickBasePosition = ccp(screenSize.width*0.07f, 
            screenSize.height*0.11f); 
     jumpButtonPosition = ccp(screenSize.width*0.93f, 
           screenSize.height*0.11f); 
     attackButtonPosition = ccp(screenSize.width*0.93f, 
            screenSize.height*0.35f); 
    } 

SneakyJoystickSkinnedBase *joystickBase = [[[SneakyJoystickSkinnedBase alloc] init] autorelease]; 
joystickBase.position = joystickBasePosition; 
joystickBase.backgroundSprite =[CCSprite spriteWithFile:@"dpadDown.png"]; 
joystickBase.thumbSprite =[CCSprite spriteWithFile:@"joystickDown.png"]; 
joystickBase.joystick = [[SneakyJoystick alloc]initWithRect:joystickBaseDimensions]; 

leftJoystick = [joystickBase.joystick retain]; 
leftJoystick.isDPad = YES; 
[self addChild:joystickBase]; 

SneakyButtonSkinnedBase *jumpButtonBase =[[[SneakyButtonSkinnedBase alloc] init] autorelease]; 
jumpButtonBase.position = jumpButtonPosition; 
jumpButtonBase.defaultSprite =[CCSprite spriteWithFile:@"jumpUp.png"]; 
jumpButtonBase.activatedSprite =[CCSprite spriteWithFile:@"jumpDown.png"]; 
jumpButtonBase.pressSprite = [CCSprite spriteWithFile:@"jumpDown.png"]; 
jumpButtonBase.button = [[SneakyButton alloc]initWithRect:jumpButtonDimensions]; 
jumpButton = [jumpButtonBase.button retain]; 
jumpButton.isToggleable = NO; 
[self addChild:jumpButtonBase]; 
SneakyButtonSkinnedBase *attackButtonBase =[[[SneakyButtonSkinnedBase alloc] init] autorelease]; 
attackButtonBase.position = attackButtonPosition; 
attackButtonBase.defaultSprite = [CCSprite spriteWithFile:@"handUp.png"]; 
attackButtonBase.activatedSprite = [CCSprite spriteWithFile:@"handDown.png"]; 
attackButtonBase.pressSprite = [CCSprite spriteWithFile:@"handDown.png"]; 
attackButtonBase.button = [[SneakyButton alloc]initWithRect:attackButtonDimensions]; 
attackButton = [attackButtonBase.button retain]; 
attackButton.isToggleable = NO; 
[self addChild:attackButtonBase]; 

> } 

    > -(void)applyJoystick:(SneakyJoystick *)aJoystick toNode:(CCNode *)tempNode forTimeDelta:(float)deltaTime{ 
    >  CGPoint scaledVelocity = ccpMult(aJoystick.velocity, 44.0f); // 1 
    >  CGPoint newPosition = ccp(tempNode.position.x + scaledVelocity.x * deltaTime,tempNode.position.y + scaledVelocity.y * deltaTime); 
    >  [tempNode setPosition:newPosition]; 
    >  if (jumpButton.active == YES) { 
    >   CCLOG(@"Jump button is pressed."); 
    >   if (attackButton.active == YES) { 
    >    CCLOG(@"Attack button is pressed."); 
    >   } } 
    >  } 

> 

    > # pragma mark - 
    > # pragma mark Update Method 

> -(void) update:(ccTime)deltaTime { 
>  [self applyJoystick:leftJoystick toNode:dude 
>   forTimeDelta:deltaTime]; 
>  } 

回答

0

我爲我的遊戲類似的東西。有很多步驟,不能深入細節。

第一個您需要知道何時使用操縱桿。

第二個您需要插入操縱桿的方向。對於像口袋妖怪這樣的遊戲,你只需要檢查上/下/右/左方向。可以訪問操縱桿的度數。簡單地解釋度,如「如果是度之間-45到45,這意味着操縱桿主要是向右」等

,你將不得不從您的播放器對象斷開鏈接的操縱桿。這個教程在某個地方讓你連接它們兩個,這樣操縱桿就會自動移動播放器。你必須撤消這個。

第四個在你的場景中的某個地方組成一個時間表。時間表將解釋遊戲杆的方向(如果它正在使用),並且將對玩家運行一個CCMoveBy動作。如果操縱桿指向右側並且地圖的圖塊大小爲32,那麼CCMoveBy移動參數將爲(32,0)。如果玩家已經在運行一個,請記住不要執行其他操作。

有很多細節和很多波蘭語。你最好試着單獨詢問每一件事,而不是一件事。