先把昨天实现的功能先写下,昨天因为各种事然后博客的够没有及时更新。
背景的滚动:
this->schedule(schedule_selector(GameScene::autoremove), 0.03f);//设置定时器
Size visibleSize = Director::getInstance()->getVisibleSize();
auto b = this->getChildByTag(10);
b->setPositionY(b->getPositionY()-10);
auto b1 = this->getChildByTag(8);
b1->setPositionY(b1->getPositionY() - 10);
实现背景的滚动主要的核心是添加计时器,然后不断的循环,y轴 的坐标是通过减少的方式是实现背景的移动,再是通过添加两张背景实现重复的功能。
添加英雄类:
//添加英雄飞机
Size visibleSize = Director::getInstance()->getVisibleSize();
plane = Sprite::create("feiji.png");
//设置起始位置
plane->setPosition(ccp(visibleSize.width / 2, visibleSize.height / 5));
this->addChild(plane);
auto action = Blink::create(5,10);//添加英雄闪烁的效果
plane->runAction(action);
这里的添加飞机主要是和以前添加背景的步骤是一样的,就是新建一个类,然后在主场景中实现调用添加就是可以的。
还有就是添加了飞机的闪烁的效果,这里有还好多的效果,都要学会用。
添加子弹类:
//添加子弹
Size visibleSize = Director::getInstance()->getVisibleSize();
auto Buttle = Sprite::create("buttle.png");
//设置起始位置
auto sss = this->plane->getPlane();
// CCLOG("sss->x:%f", sss->getPositionX());
this->addChild(Buttle);
这里添加的子弹都是一样的,注意设置子弹的起始位置还有就是让子弹实现自动消失,自动清除内存。
auto move = MoveTo::create(2, ccp(sss->getPositionX(), visibleSize.height + Buttle->getContentSize().height / 2));
Buttle->runAction(move);
实现自动清除内存。
实现子弹的连续发射:
void Buttle::move(){
this->schedule(schedule_selector(Buttle::moveto), 0.5f);//设置定时器
}
void Buttle::moveto(float dt){
//添加子弹
Size visibleSize = Director::getInstance()->getVisibleSize();
auto Buttle = Sprite::create("buttle.png");
//设置起始位置
auto sss = this->plane->getPlane();
// CCLOG("sss->x:%f", sss->getPositionX());
this->addChild(Buttle);
Buttle->setPosition(ccp(sss->getPosition().x, sss->getPosition().y+80));
Buttle->runAction(move);
要实现飞机的连续发射就是用一个计时器,然后用通过Move 的动作实现发射
这样就可以实现子弹的连续发射。
实现点击,移动的功能:
//添加点击事件
// Make sprite1 touchable
auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);
listener1->onTouchBegan = [](Touch* touch, Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
//Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
if (rect.containsPoint(target->convertToNodeSpace(touch->getLocation())))
{
//log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
target->setOpacity(255);
return true;
}
return false;
};
listener1->onTouchMoved = [](Touch* touch, Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setPosition(target->getPosition() + touch->getDelta());
。。。。。。。
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, plane);
添加触摸事件,然后有事件的监听,实现移动的功能。
实现飞机的边界对问题:
if (target->getPositionX()<= target->getContentSize().width / 2)
{
float xx = target->getContentSize().width / 2;
target->setPositionX(xx);
}
else if (target->getPositionY() + touch->getDelta().y <= target->getContentSize().height / 2){
target->setPositionY(target->getContentSize().height / 2);
}
else{
target->setPositionY(target->getPosition().y + touch->getDelta().y);
}
对边界的判断实现飞机判断,对于外面的不能触摸的。
实现飞机和子弹的绑定:
子弹还有飞机的绑定主要是有一个单例模式。主要是把子弹在飞机类中可以实现调用。
Buttle::Buttle(Plane *p){
this->plane = p;
move();
}
void Buttle::move(){
this->schedule(schedule_selector(Buttle::moveto), 0.5f);//设置定时器
}
Plane * Plane::p = NULL;
Plane* Plane:: show(){
if (NULL==p)
{
p=new Plane();
}
return p;
}
Plane::Plane()
{
createInit();
}
还最重要的是创建静态创建对象,静态的方法
还有就是绑定的时候,记得飞机是全局的变量,这样才会被其他的访问。