2011-11-26 112 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)。如果玩家已经在运行一个,请记住不要执行其他操作。

有很多细节和很多波兰语。你最好试着单独询问每一件事,而不是一件事。