Cocos2d iphone Cocos2d:CCNode的控制器调用方法

Cocos2d iphone Cocos2d:CCNode的控制器调用方法,cocos2d-iphone,Cocos2d Iphone,我的GameController.m调用CCNode类的一个方法。它似乎调用它,因为我可以看到NSLog,但该方法不是operate或not result in view。换句话说,当从GameController调用方法时,屏幕不会改变。方法在其自己的类中自行操作 我应该如何更改代码来修复它 总结: GameController->calls->GoalPost.守门员.method 代码: 游戏控制器.m,门柱.m和守门员.m GameController.m #import "GameCo

我的GameController.m调用CCNode类的一个方法。它似乎调用它,因为我可以看到NSLog,但该方法不是operate或not result in view。换句话说,当从GameController调用方法时,屏幕不会改变。方法在其自己的类中自行操作

我应该如何更改代码来修复它

总结: GameController->calls->GoalPost.守门员.method

代码: 游戏控制器.m,门柱.m和守门员.m

GameController.m

#import "GameController.h"
#import "cocos2d-ui.h"
#import "GoalPost.h"
#import "Ball.h"
#import "Field.h"

@interface GameController()
@property Field* field;
//@property Ball* ball;
@property GoalPost* post;
@end

@implementation GameController

- (instancetype)init
{
self = [super init];
if (self) {
    self.field = [Field node];
//        self.ball = [Ball node];
    self.post = [GoalPost node];

}
return self;
}

//control computer.
-(void)kickerTurn{
NSLog(@"Player kick");
//random location generation
//call keeper.
int randomKeeperX = arc4random() % (int)self.post.contentSize.width;
int randomKeeperY = arc4random() % (int)self.post.contentSize.height;
CGPoint keeperLoc = ccp(randomKeeperX, randomKeeperY);

[self.post.keeper keeperLocation:keeperLoc];
}

@end
球门柱

#import "GoalKeeper.h"
@interface GoalPost : CCNodeColor {   }
@property GoalKeeper* keeper;
@end
门柱

#import "GoalPost.h"
#import "GoalKeeper.h"
@interface GoalPost ()
@property NSArray* post;
@end

@implementation GoalPost
- (instancetype)init
{
self = [super initWithColor:[CCColor lightGrayColor]];
if (self) {
    ....
        [self addChild:aPost];
    }

    //Call goal keeper and add.
    //for touch to run goal keeper.
    [self setUserInteractionEnabled:true];
    self.keeper = [GoalKeeper node];

    //Put it in front of goal post.
    self.keeper.position = ccp(self.contentSize.width/2, 0);
    [self addChild:self.keeper];

}
return self;
}

-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchLoc = [touch locationInNode:self];
NSLog(@"Goal touched: %f %f", touchLoc.x, touchLoc.y);
//Goal keeper call
[self.keeper keeperLocation:touchLoc];

}

@end
最后,守门员

h 导入cocos2d.h

@interface GoalKeeper : CCNodeColor { }
-(void)keeperLocation:(CGPoint)location;
@end
m


多久调用一次keeperLocation?如果它每帧都发生,它将无法工作,因为动作需要时间才能生效,另外,如果移动动作已经在运行,它们将相互干扰other@LearnCocos2D这应该发生一次。就像AppDelegate启动场景时,场景调用GameController一样。我将在需要时添加更多控件来调用keeperLocation。
#import "GoalKeeper.h"
@interface GoalKeeper()
@property CGPoint touchLoc;
@end

@implementation GoalKeeper

- (instancetype)init
{
self = [super initWithColor:[CCColor blueColor]];
if (self) {
    [self setUserInteractionEnabled:TRUE];
    [self setContentSize:CGSizeMake(30, 70)];

}
return self;
}

-(void)keeperLocation:(CGPoint)location {

CGPoint offset = ccpSub(location, self.position);
int targetX = self.position.x + offset.x;
int targetY = self.position.y + offset.y;
CGPoint targetPosition = ccp(targetX, targetY);

CCActionMoveBy *actionMoveBy = [CCActionMoveBy actionWithDuration:0.5f position:offset];
[self runAction:actionMoveBy];
}

@end