本部分內容設定了隱藏,需要回復後才能看到
1
|
USING_NS_CC;
|
1
2
3
|
bool
onTouchBegan(Touch* touch, Event* event);
void
onTouchMoved(Touch* touch, Event* event);
void
onTouchEnded(Touch* touch, Event* event);
|
1
2
3
4
5
6
7
8
9
10
|
//SET MOUSE LISTENER
auto
listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(
true
);
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan,
this
);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved,
this
);
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded,
this
);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,
this
);
//END MOUSE LISTENER
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
bool
HelloWorld::onTouchBegan(Touch* touch, Event* event)
{
return
true
;
}
void
HelloWorld::onTouchMoved(Touch* touch, Event* event)
{
}
void
HelloWorld::onTouchEnded(Touch* touch, Event* event)
{
}
|
1
2
3
4
5
6
7
8
9
10
11
|
//CREATE A BALL
dragOffsetStartX = 0;
dragOffsetEndX = 0;
dragOffsetStartY = 0;
dragOffsetEndY = 0;
existBall=
false
;
ballX = 500;
ballY = 200;
ball =Sprite::create(
"ball.png"
);
ball->setPosition(CCPoint(ballX,ballY));
this
->addChild(ball);
|
1
2
3
4
5
6
7
8
9
10
11
12
|
Sprite *ball;
bool
existBall;
float
ballX;
float
ballY;
int
dragOffsetStartX;
int
dragOffsetEndX;
int
dragOffsetStartY;
int
dragOffsetEndY;
b2Body *ballBody;
b2CircleShape ballShape;
b2BodyDef ballBodyDef;
void
defineBall();
|
1
|
#include <Box2D/Box2D.h>
|
1
|
class
HelloWorld :
public
cocos2d::Layer
|
1
|
class
HelloWorld :
public
cocos2d::Layer,
public
b2ContactListener
|
1
2
|
b2World *world;
float
deltaTime;
|
1
2
|
b2Vec2 gravity = b2Vec2(0.0f, -10.0f);
world =
new
b2World(gravity);
|
1
|
#define SCALE_RATIO 32.0
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
void
HelloWorld::defineBall() {
ballShape.m_radius = 45 / SCALE_RATIO;
b2FixtureDef ballFixture;
ballFixture.density=10;
ballFixture.friction=0.8;
ballFixture.restitution=0.6;
ballFixture.shape=&ballShape;
ballBodyDef.type= b2_dynamicBody;
ballBodyDef.userData=ball;
ballBodyDef.position.Set(ball->getPosition().x/SCALE_RATIO,ball->getPosition().y/SCALE_RATIO);
ballBody = world->CreateBody(&ballBodyDef);
ballBody->CreateFixture(&ballFixture);
ballBody->SetGravityScale(10);
}
|
1
|
HelloWorld::defineBall();
|
1
|
void
update(
float
dt);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//Simulate Physics
void
HelloWorld::update(
float
dt){
int
positionIterations = 10;
int
velocityIterations = 10;
deltaTime = dt;
world->Step(dt, velocityIterations, positionIterations);
for
(b2Body *body = world->GetBodyList(); body != NULL; body = body->GetNext())
if
(body->GetUserData())
{
CCSprite *sprite = (CCSprite *) body->GetUserData();
sprite->setPosition(ccp(body->GetPosition().x * SCALE_RATIO,body->GetPosition().y * SCALE_RATIO));
sprite->setRotation(-1 * CC_RADIANS_TO_DEGREES(body->GetAngle()));
}
world->ClearForces();
world->DrawDebugData();
}
|
1
|
scheduleUpdate();
|
1
|
void
addWall(
float
w,
float
h,
float
px,
float
py);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
void
HelloWorld::addWall(
float
w,
float
h,
float
px,
float
py) {
b2PolygonShape floorShape;
floorShape.SetAsBox(w/ SCALE_RATIO,h/ SCALE_RATIO);
b2FixtureDef floorFixture;
floorFixture.density=0;
floorFixture.friction=10;
floorFixture.restitution=0.5;
floorFixture.shape=&floorShape;
b2BodyDef floorBodyDef;
floorBodyDef.position.Set(px/ SCALE_RATIO,py/ SCALE_RATIO);
b2Body *floorBody = world->CreateBody(&floorBodyDef);
floorBody->CreateFixture(&floorFixture);
}
|
1
2
3
|
addWall(visibleSize.width ,10,(visibleSize.width / 2) ,0);
//CEIL
addWall(10 ,visibleSize.height ,0,(visibleSize.height / 2) );
//LEFT
addWall(10 ,visibleSize.height ,visibleSize.width,(visibleSize.height / 2) );
//RIGHT
|
1
|
Sprite *points[32];
|
1
2
3
4
|
for
(
int
i = 1 ; i <= 31; i++){
points<i> =CCSprite::create(
"dot.png"
);
this
->addChild(points<i>);
}</i></i>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
dragOffsetStartX = touch->getLocation().x;
dragOffsetStartY = touch->getLocation().y;
CCPoint touchLocation = touch->getLocation();
ballX = touchLocation.x;
ballY = touchLocation.y;
if
(existBall){
world->DestroyBody(ballBody);
}
ball->setPosition(ccp(ballX ,ballY));
|
1
2
3
4
5
6
7
8
9
|
CCPoint touchLocation = touch->getLocation();
dragOffsetEndX = touchLocation.x;
dragOffsetEndY = touchLocation.y;
float
dragDistanceX = dragOffsetStartX - dragOffsetEndX;
float
dragDistanceY = dragOffsetStartY - dragOffsetEndY;
HelloWorld::simulateTrajectory(b2Vec2((dragDistanceX )/SCALE_RATIO,(dragDistanceY )/SCALE_RATIO));
|