我有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];
> }