2011-05-05 41 views
1

我有一个精灵,我需要用触摸旋转它,但它位于不同的层。是否有可能更新它的位置?从另一个层面安排更新

E.G. 雪碧有它自己的层,但它的位置需要更新主游戏内

这是我到目前为止。

#import <Foundation/Foundation.h> 
#import "cocos2d.h" 
#import "GameScene.h" 



@interface G : CCLayer { 

    CCSprite *g; 

    CGFloat gRotation; 
} 

@end 

------------------------------------------ 
#import "G.h" 


@implementation G 

-(id) init 
{ 
    if ((self = [super init])) 
    { 
     CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); 

     g = [CCSprite spriteWithFile:@"g.png"]; 

     [self addChild:g z:-1]; 

     //[self scheduleUpdate]; 
     if (g.rotation == 360) 
     { 
      [self unscheduleUpdate]; 
     } 
    } 
    return self; 
} 

- (void)update:(ccTime)delta 
{ 
    g.rotation = gRotation; 
} 

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

} 

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 

    CGPoint firstLocation = [touch previousLocationInView:[touch view]]; 
    CGPoint location = [touch locationInView:[touch view]]; 

    CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location]; 
    CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation]; 

    CGPoint firstVector = ccpSub(firstTouchingPoint, g.position); 
    CGFloat firstRotateAngle = -ccpToAngle(firstVector); 
    CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle); 

    CGPoint vector = ccpSub(touchingPoint, g.position); 
    CGFloat rotateAngle = -ccpToAngle(vector); 
    CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle); 

    gRotation += currentTouch - previousTouch; 
} 

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

} 

- (void) dealloc 
{ 
    CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); 

    [super dealloc]; 
} 

@end 

GameScene

#import "GameScene.h" 
#import "MainMenu.h" 
#import "G.h" 


@implementation GameScene 

+(CCScene *) scene 
{ 
    CCScene *scene = [CCScene node]; 
    GameScene *layer = [GameScene node]; 

    [scene addChild: layer]; 
    return scene; 
} 

-(void) tapG: (id) sender 
{ 

    G *gView; 
    gView = [[G alloc] init]; 
    gView.position = ccp(100, 100); 

    [self.parent addChild:gView z:1001]; 

    [gView schedule:@selector(update:)]; 

    [gView release]; 
} 
-(id) init 
{ 
    if ((self = [super init])) 
    { 
tG = [CCMenuItemImage itemFromNormalImage:@"tp.png" selectedImage:@"tp.png" disabledImage:@"tpaperd.png" target:self selector:@selector(tapG:)]; 

     gt = [CCMenu menuWithItems:tG, nil]; 
     gt.position = ccp(210, 80); 
     [gt alignItemsHorizontallyWithPadding:10]; 

     [self addChild:gt z:0]; 

    } 
    return self; 
} 
- (void) dealloc 
    { 
     CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); 

     [super dealloc]; 
    } 

它带来了精灵,但精灵不转动。

回答

0

您可以安排其他图层的更新。

您确定您的G图层设置为启用触摸吗?分配gView后尝试gView.isTouchEnabled = YES;。您可能需要确认协议。添加<CCStandardTouchDelegate>您CCLayer接口:@interface G : CCLayer <CCStandardTouchDelegate> { ....

你也可以计划使用[gView scheduleUpdate];它会产生相同的结果,但它更方便-(void)update:(ccTime)delta方法。

+0

工作!谢谢! – Jonathan 2011-05-05 13:56:13