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 of multiple moving balls