因为我是 cocos2d 的新手,而且我很挣扎。任何人都可以建议我如何解决这个问题。
我有 3 个盒子(它们是运动体)
还有多个球(它们是动态体),每个球都有一个标签值(框号)。
我在投球的地方和盒子之间有一些障碍物(运动体)。
场景是我将随机获得盒子编号,例如:2(第二个盒子)并设置为球的标签值。即使球碰到障碍物,它也应该准确地进入第二个盒子。如果我得到下一个球的盒子编号为 3,那么球应该进入第三个盒子。
任何人都可以请建议我,
谢谢你,莫尼什
因为我是 cocos2d 的新手,而且我很挣扎。任何人都可以建议我如何解决这个问题。
我有 3 个盒子(它们是运动体)
还有多个球(它们是动态体),每个球都有一个标签值(框号)。
我在投球的地方和盒子之间有一些障碍物(运动体)。
场景是我将随机获得盒子编号,例如:2(第二个盒子)并设置为球的标签值。即使球碰到障碍物,它也应该准确地进入第二个盒子。如果我得到下一个球的盒子编号为 3,那么球应该进入第三个盒子。
任何人都可以请建议我,
谢谢你,莫尼什
这是一个简单的帮助类来创建球并返回。您可以使用类似的方法制作盒子。
//
// Ball.h
// BouncingBall
//
// Created by Omar Hussain on 8/12/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"
class Ball : public CCSprite {
public:
CCSprite *sprite;
b2BodyDef ballBodyDef;
b2Body *ballBody;
b2FixtureDef ballShapeDef;
b2Fixture *fixture;
Ball(b2World *world);
b2Vec2 disp;
CCSprite *shadowBall;
b2World *world;
bool isAnimating;
void SetBallOverlap(bool willOverlap, int scale);
void Destroy();
};
#import "Ball.h"
#define Density 1.0f
#define Friction 1.0f
#define Restitution 0.8f
#define PTM_RATIO 32
#define SpriteRadius 40.5
Ball::Ball(b2World *world){
this->world = world;
this->sprite = [CCSprite spriteWithFile:@"5.png"];
[sprite setPosition:ccp(160,460)];
this->isAnimating = false;
//Set up sprite
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(160/PTM_RATIO, 460/PTM_RATIO);
ballBodyDef.userData = sprite;
this->ballBody = world->CreateBody(&ballBodyDef);
b2CircleShape circle;
circle.m_radius = SpriteRadius/PTM_RATIO;
ballShapeDef.shape = &circle;
ballShapeDef.density = Density;
ballShapeDef.friction =Friction;
ballShapeDef.restitution = Restitution;
this->fixture = this->ballBody->CreateFixture(&ballShapeDef);
//this->shadowBall = [TSprite spriteWithFile:@"ball_shadow.png"];
}
void Ball::SetBallSize(int scale){
this->ballBody->DestroyFixture(fixture);
this->world->DestroyBody(ballBody);
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(160/PTM_RATIO, 460/PTM_RATIO);
ballBodyDef.userData = sprite;
this->ballBody = world->CreateBody(&ballBodyDef);
float correctedScale = scale/9.0;
b2CircleShape circle;
circle.m_radius = (correctedScale *SpriteRadius)/PTM_RATIO;
ballShapeDef.shape = &circle;
ballShapeDef.density = Density;
ballShapeDef.friction = Friction;
ballShapeDef.restitution = Restitution;
this->fixture = this->ballBody->CreateFixture(&ballShapeDef);
//this->shadowBall.scale = scale;
this->sprite.scale = correctedScale;
}
void Ball::SetBallOverlap(bool willOverlap, int scale){
this->world->DestroyBody(ballBody);
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(160/PTM_RATIO, 460/PTM_RATIO);
ballBodyDef.userData = sprite;
this->ballBody = world->CreateBody(&ballBodyDef);
float correctedScale = scale/9.0;
b2CircleShape circle;
circle.m_radius = (correctedScale *SpriteRadius)/PTM_RATIO;
ballShapeDef.shape = &circle;
ballShapeDef.density = Density;
ballShapeDef.friction = Friction;
ballShapeDef.restitution = Restitution;
if(willOverlap)
ballShapeDef.filter.groupIndex = 1;
else
ballShapeDef.filter.groupIndex = -1;
this->ballBody->CreateFixture(&ballShapeDef);
}
void Ball::Destroy(){
this->ballBody->DestroyFixture(fixture);
this->world->DestroyBody(ballBody);
}