[一位菜鸟的COCOS-2D编程之路]COCOS2D基础知识-CCNode节点类/CCNode节点类方法

本文主要介绍了COCOS2D-x编程中的基础组件CCNode,包括其作为节点类的主要属性,特别是子节点的管理和操作。通过对CCNode的理解,开发者能够更好地构建游戏场景和交互元素。
摘要由CSDN通过智能技术生成

1.CCNode节点类的属性

//锚点--------------------------------
        CCLabelTTF *lifeLabel = [CCLabelTTF labelWithString:@"生命值:" fontName:@"Arial" fontSize:20];
        //锚点设置
        lifeLabel.anchorPoint = CGPointMake(1, 1); //把postion 点作为label的左上角
        
        lifeLabel.position = ccp(120, winSize.height - 120);
        
        [self addChild:lifeLabel z:10];
        
        CCLabelTTF *lifeLabel2 = [CCLabelTTF labelWithString:@"生命值2:" fontName:@"Arial" fontSize:20];
        //锚点设置
        lifeLabel2.anchorPoint = CGPointMake(0.5, 0.5);//把postion 点作为label的几何中心
        lifeLabel2.position = ccp(120, winSize.height - 120);
        
        [self addChild:lifeLabel2 z:10];
        
        CCLabelTTF *lifeLabel3 = [CCLabelTTF labelWithString:@"生命值3:" fontName:@"Arial" fontSize:20];
        //锚点设置
        lifeLabel3.anchorPoint = CGPointMake(0, 0);把postion 点作为label的右下角
        
        lifeLabel3.position = ccp(120, winSize.height - 120);
        
        [self addChild:lifeLabel3 z:10];
        


//CCNode 节点类-------------------
        /*节点类的属性
        1.anchorPoint 锚点
         2.camera 游戏视角 3d使用
         3.children 子节点.  (addchild:)children 为节点的子节点数组,变量类型是CCArray
         4.contentSize  //未转化的节点大小,也就是本身的宽,高 :主要用于碰撞检测
         */
        //得到精灵的矩形区域
        
        CCLabelTTF *label = [CCLabelTTF labelWithString:@"sprite" fontName:@"Arial" fontSize:20];
        
        label.position = ccp(100, 100);
        [self addChild:label];
        
        CCSprite *sprite = [[CCSprite alloc]initWithFile:@"hero_1.png"];
        
        sprite.position = ccp(50, 50);
        
        [self addChild:sprite];
        
        //NSLog(@"%@",[self rectOfSprite:sprite]);
        NSLog(@"(%f,%f,%f,%f)",[self rectOfSprite:sprite].origin.x,[self rectOfSprite:sprite].origin.y,[self rectOfSprite:sprite].size.width,[self rectOfSprite:sprite].size.height);
        //(9.000000,-10.500000,82.000000,121.000000)


得到精灵位置的类->用于碰撞检测

//这个矩形位置 是依赖  左下角作为原点进行的
- (CGRect)rectOfSprite:(CCSprite *)sprite
{
    //依赖位置坐标和contentSize进行转换,默认的锚点是 (0.5 ,0.5)
    return CGRectMake(sprite.position.x - sprite.contentSize.width/2, sprite.position.y - sprite.contentSize.height/2, sprite.contentSize.width, sprite.contentSize.height);

}



2.CCNode节点类的方法

1)子节点的处理

  //1.子节点的处理--------------
         //1)创建一个node
        CCNode *node = [CCNode node];
        //2增加一个子节点
        CCNode *childNode = [CCNode node];
        [node addChild:childNode z:10 tag:1];
        //3使用tag获得子节点
        CCNode *tagNode = [node getChildByTag:1];//等于 childNode
        //4使用tag删除子节点
        [node removeChildByTag:1];//删除了childNode
        //5通过节点指针删除节点
        [node removeChild:childNode cleanup:YES];//如果cleanup 为yes,则停止运行中的任何动作
        //6删除一个节点的所有节点
        [node removeAllChildrenWithCleanup:YES];
        //7从当前节点的父节点删除当前节点
        [childNode removeFromParentAndCleanup:YES];

2)子节点执行的动作

   //2执行动作 -----------------
        id action = [CCAction action];
        [action setTag:2];
        //1运行action动作
        [node  runAction:action];
        //2停止该节点的所有动作
        [node stopAllActions];
        //3停止某个特定的动作/依赖tag
        [node stopAction:action];
        [node stopActionByTag:2];
        //4获取当前节点的某个特定动作
        id tempAction = [node getActionByTag:2];
        //5获取某节点的所有动作的数量
        int numberOfActions = [node numberOfRunningActions];

3)预定消息

//3预定消息
//1)每帧都调用的更新方法
- (void)scheduleUpdates
{
    [self scheduleUpdate];

}

- (void)update:(ccTime)delta
{
   //游戏中每一帧都将调用此方法
}

//2)为消息安排优先级  从小到大开始执行,顺序是  3-> 1 ->2
//1)每帧都调用的更新方法
- (void)scheduleUpdates1
{
    [self scheduleUpdate];
}
- (void)scheduleUpdates2
{
    [self scheduleUpdateWithPriority:1];
}
- (void)scheduleUpdates3
{
    [self scheduleUpdateWithPriority:-1];
}


//3)指定运行特定的更新方法 并设置调用的时间间隔
- (void)scheduleUpdates4
{
    [self schedule:@selector(updateTenTimesPerSecond:) interval:0.1f];
}

- (void)updateTenTimesPerSecond:(ccTime)delta
{

}

        //4)停止某个节点的某个指定选择器,但是不会停止 预定的更新方法
        [self unschedule:@selector(updateTenTimesPerSecond:)];
        //如果停止预定的更新方法
        [self unscheduleUpdate];
        //5停止节点的所有选择器,保罗 schduleupdate里设置的Update 选择期.但是不会停止节点的动作
        [self unscheduleAllSelectors];
        
        //6)_cmd 关键字停止当前方法的预定
          [self unschedule:_cmd];
        

4.其他的方法:(不完全)

     //4.其他方法
        //1) 获取节点的边框
       CGRect spriteBound = [sprite boundingBox];//==rectOfSprite
        //
        //2)清除所有的动作和预定的方法
        [sprite cleanup];//
        //坐标转换....
        //3)绘制自己的节点
        // - (void)draw{};
        //4)-(id)init 初始化方法
        //5 - (void)onEnter 回调方法:当 CCNode 进入舞台的时候调用
        
        //6 - (void)onEnterTransitionDidFinished //带有过渡效果的进入舞台的时候调用
        // - (void)OnExit  //离开舞台时候调用


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值