因为我是cocos2d的新手,我正在努力奋斗。任何人都建议我如何解决这个问题。
我有3个盒子(运动体)
还有多个球(动态体),每个球都有一个标签值(箱号)。
我在射球的位置和方块之间有一些障碍(运动体)。
场景是我将随机获得箱号:2(第二个方框)并将其设置为球的标签值。并且球应该完全进入第二个方框,即使它击中了障碍物。如果我得到下一个球的方框号为3,那么球应该进入第三个方框。
任何人都可以建议我,
谢谢你, Monish
答案 0 :(得分:1)
这是一个创建球和返回的简单助手类。您可以使用类似的方法制作盒子。
//
// 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);
}