SneakyJoystick - 某些点/瓦片的移动

时间:2011-11-26 13:18:03

标签: iphone cocos2d-iphone

我已经开始运行SneakyJoystick,但是我希望以特定的频率按平铺移动我的精灵图块...我对iphone编程和cocos2d都很新,所以我不知道整个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];
>      }

1 个答案:

答案 0 :(得分:0)

我为我的游戏做了类似的事情。有很多步骤,不能详细说明。

首先您需要知道何时使用操纵杆。

第二次你需要插入操纵杆的方向。对于像口袋妖怪这样的游戏,您只需要检查上/下/右/左方向。可以访问操纵杆的。简单地解释像"如果度在-45到45之间,则意味着操纵杆主要面向右边"等

第三次您必须取消操纵杆与播放器对象的链接。那个教程,在某个地方,让你链接它们,以便操纵杆将自动移动播放器。你必须撤消它。

第四在您的场景中的某个地方制作计划。计划将解释操纵杆方向(如果正在使用),并将对玩家进行CCMoveBy操作。如果操纵杆指向右侧并且地图的平铺大小为32,则CCMoveBy移动参数将为(32,0)。如果玩家已经在运行,请记住不要运行其他动作。

有很多细节和很多东西要擦亮。你最好尝试单独询问每件事而不是一次性拍摄所有东西。