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);
}
}