构建好主角类后,接下来我们就需要去将类中定义的各种操作进行完善,首先我们进行主角小猫初始化函数onEnter()的实现,代码如下:
#include "GameObjHero.h"
using namespace cocos2d;
GameObjHero::GameObjHero(void)
{
}
GameObjHero::~GameObjHero(void)
{
}
void GameObjHero::onEnter()
{
CCNode::onEnter();
this->setContentSize(CCSizeMake(85 , 90));
CCDirector* pDirector = CCDirector::sharedDirector(); //获取对窗口进行操作的句柄
pDirector->getTouchDispatcher()->addTargetedDelegate(
this,//在那个类中实现触屏回调函数
0, //优先级
true);//触摸时间是否被该目标截获
CCSprite *mainsprite = CCSprite::create("catBody1.png");
//主体动画
CCAnimation* animation = CCAnimation::create();
animation->addSpriteFrameWithFileName("catBody1.png"); //添加动画桢
animation->addSpriteFrameWithFileName("catBody2-4.png");
animation->addSpriteFrameWithFileName("catBody3.png");
animation->addSpriteFrameWithFileName("catBody2-4.png");
animation->setDelayPerUnit(0.1f); //设置动画桢之间的延迟时间
animation->setRestoreOriginalFrame(true);
mainsprite->runAction(CCRepeatForever::create(CCAnimate::create(animation))); //执行一个延时动作
addChild(mainsprite); //加入子节点
//尾巴动画
CCSprite *tail = CCSprite::create("catTail.png");
tail->setAnchorPoint(ccp(0.5 , 1));
tail->setPosition(ccp(-5 , -29));
tail->setScale(0.5); //缩放
tail->setRotation(20);
tail->runAction(CCRepeatForever::create((CCActionInterval*)CCSequence::create(CCRotateBy::create(0.5 , -40) , CCRotateBy::create(0.5 ,40) , NULL)));
addChild(tail);
//手部动画
lefthand = CCSprite::create("catHand1.png");
lefthand->setAnchorPoint(ccp(1 , 0.5));
lefthand->setPosition(ccp(-18 , -20));
addChild(lefthand);
righthand = CCSprite::create("catHand2.png");
righthand->setPosition(ccp(18 , -20));
righthand->setAnchorPoint(ccp(0 , 0.5));
addChild(righthand);
offset = ccp(0 , 0);
iscontrol = false;
//schedule(schedule_selector(GameObjHero::releasebullet) , 1.0f);
}
编译后出现了几个错误,有些事自己看错导致的,花了二十几分钟才找到,果然有VAssistX方便是方便,但总是容易出错。但还有出现的两个错误没有解决:
1.是头文件中 "#endif"出现“ fatal error C1020: 意外的 #endif”的错误,网上搜了一通,没有找到解决办法,想来想去以为是包含头文件“cocos2d.h"的位置关系,试了下还是有错,现在也只能说是加了cpp文件后导致的,目前还没发现原因,郁闷!
2.是上述代码”schedule(schedule_selector(GameObjHero::releasebullet) , 1.0f)“出现error C2440: “类型转换”: 无法从“void (__thiscall GameObjHero::* )(void)”转换为“cocos2d::SEL_SCHEDULE”的错,虽然还没去探究该函数的具体用法,但参考了一下别的代码,貌似支持void类型啊,我还试了下强制转换,也不行,疯了,明天再好好,找找。
(ps:2013.08.15 今天编译程序的时候突然发现了上述错误一的原因:自己将“#ifndef #define ... #endif” 错写成了“#ifndef #endif ... #endif”的结构,上次居然没看出来,汗颜啊。)
-----------------------------------------------------------------------------------------------------------------------------
算了,还没早点错误,先暂时屏蔽掉吧,后面具体要用的时候再去分析吧。
知识点:
1.onEnter()函数:
官方解释:Event callback that is invoked every time whenNode enters the 'stage'. 字面上的意思就是说”该节点每次进入舞台就会被调用“,其主要作用就是用来初始化类中的一些操作,与init()函数作用类似,但有区别:
init()和onEnter()这俩个方法都是CCNode的方法。
1>其被调用的顺序是先init(),后onEnter()。
2>init()在类的初始化时只会调用一次。
3>onEnter在该类被载入场景的时候被调用,可能会发生多次。
4>CCLayer* cclayer = new MyLayer();
这种情况下,只会触发onEnter。
5>CCLayer* cclayer = MyLayer::create();
这样情况下,既会触发init()方法,也会触发onEnter()方法。
2.CCAnimation类
使用一个CCAnimation对象可以CCSprite对象上执行动画。 CCAnimation对象包含CCSpriteFrame对象,与帧之间可能出现的延误。你可以使用CCAnimate的方法 播放CCAnimation对象。
3.setAnchorPoint的作用”
了解这个我们必须先了解AnchorPoint(锚点)这个概念, 锚点是定位和变换操作的一个重点。锚点我们可以看成用一根图钉将一张纸或者相片钉在墙上的那个点。
节点的位置是由我们设置的position和anchor point一起决定的。
值得一提的是,anchor point的默认位置Layer的是左下,而其他的node是中点。
节点的原点是父节点的左下角。
具体附上链接:http://blog.csdn.net/xuguangsoft/article/details/8425623
http://blog.csdn.net/x_zerger/article/details/7454403里面有张图让你很容易理解锚点的概念
4.setRotation()函数
setRotation(angle) 其中angle为角度不是弧度。正数为顺时针旋转,负数为逆时针旋转。