cocos2d-x之多个移动的小球
Posted 一生不过烟花
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cocos2d-x之多个移动的小球相关的知识,希望对你有一定的参考价值。
MoveBalls.hpp:
#ifndef MoveBalls_hpp
#define MoveBalls_hpp
#include <stdio.h>
#include "cocos2d.h"
#include "Ball.hpp"
USING_NS_CC;
class MoveBalls:public LayerColor{
public:
virtual bool init();
CREATE_FUNC(MoveBalls);
virtual void update(float dt);
static Scene * createScene();
private:
// Ball* ball;
cocos2d::Vector<Ball*> balls;
void addBall(float x,float y);
};
#endif /* MoveBalls_hpp */
MoveBalls.cpp:
#include "MoveBalls.hpp"
Scene * MoveBalls::createScene(){
auto s=Scene::create();
auto l=MoveBalls::create();
s->addChild(l);
return s;
}
bool MoveBalls::init(){
LayerColor::initWithColor(Color4B(255,255,255,255));
addBall(100,100);
addBall(100,200);
addBall(100,300);
addBall(100,400);
scheduleUpdate();
return true;
}
void MoveBalls::update(float dt){
// ball->move();
for (auto it=balls.begin();it!=balls.end();it++) {
(*it)->move();
}
}
void MoveBalls::addBall(float x,float y){
auto ball=Ball::create();
ball->setPosition(x,y);
addChild(ball);
balls.pushBack(ball);
}
Ball.hpp:
#ifndef Ball_hpp
#define Ball_hpp
#include <stdio.h>
#include "cocos2d.h"
USING_NS_CC;
class Ball:public Sprite{
public:
virtual bool init();
void move();
CREATE_FUNC(Ball);
private:
float speedX;
Size visibleSize;
};
#endif /* Ball_hpp */
Ball.cpp:
#include "Ball.hpp"
bool Ball::init(){
Sprite::initWithFile("GOODS_3201_64.png");
visibleSize=Director::getInstance()->getVisibleSize();
speedX=3;
return true;
}
void Ball::move(){
setPositionX(getPositionX()+speedX);
if (getPositionX()>visibleSize.width) {
setPositionX(0);
}
}
以上是关于cocos2d-x之多个移动的小球的主要内容,如果未能解决你的问题,请参考以下文章