cocos3d动态创建3D盒子
我是cocos3d的新手,但我了解cocos2d。我想动态创建 3d 盒子。所以我在 cc3layer 中做了什么
-(void) initializeControls {
[self schedule:@selector(create_box:) interval:2 ];
}
-(void)create_box:(id)sender{
[self unschedule:@selector(mov_cel:)];
[[testWorld sharedcontescWorld] world_create_box];
}
,在 cc3world 类中做了什么,
static testWorld *_sharedcontescWorld=nil;
+(testWorld *)sharedcontescWorld{
@synchronized([testWorld class]){
if (!_sharedcontescWorld)
[self alloc];
return _sharedcontescWorld;
}return nil;
}
+(id)alloc{
@synchronized([testWorld class]) {
_sharedcontescWorld = [super alloc];
return _sharedcontescWorld;
}return nil;
}
-(void) world_create_box{
int minx=-50;
int maxx=50;
float posx=(float)(minx+arc4random()%maxx);
CC3MeshNode* aNode;
aNode = [CC3BoxNode nodeWithName: @"Simple box"];
CC3BoundingBox bBox;
bBox.minimum = cc3v(-10.0, -10.0, -10.0);
bBox.maximum = cc3v( 10.0, 10.0, 10.0);
[aNode populateAsSolidBox: bBox];
[aNode setLocation:cc3v(posx,0,0)];
aNode.material = [CC3Material material];
[self addChild:aNode];
id move3d=[CC3MoveTo actionWithDuration:1 moveTo:cc3v(posx,0,100)];
id remove=[CCCallFuncND actionWithTarget:self selector:@selector(removeObj:)];
[aNode runAction:[CCSequence actions:move3d,remove,nil]];
}
但它不起作用......任何人都可以帮助我吗?
I am new in cocos3d but i know cocos2d. i want to create 3d box dynamically. so what i did inside cc3layer is
-(void) initializeControls {
[self schedule:@selector(create_box:) interval:2 ];
}
-(void)create_box:(id)sender{
[self unschedule:@selector(mov_cel:)];
[[testWorld sharedcontescWorld] world_create_box];
}
and in cc3world class is
static testWorld *_sharedcontescWorld=nil;
+(testWorld *)sharedcontescWorld{
@synchronized([testWorld class]){
if (!_sharedcontescWorld)
[self alloc];
return _sharedcontescWorld;
}return nil;
}
+(id)alloc{
@synchronized([testWorld class]) {
_sharedcontescWorld = [super alloc];
return _sharedcontescWorld;
}return nil;
}
-(void) world_create_box{
int minx=-50;
int maxx=50;
float posx=(float)(minx+arc4random()%maxx);
CC3MeshNode* aNode;
aNode = [CC3BoxNode nodeWithName: @"Simple box"];
CC3BoundingBox bBox;
bBox.minimum = cc3v(-10.0, -10.0, -10.0);
bBox.maximum = cc3v( 10.0, 10.0, 10.0);
[aNode populateAsSolidBox: bBox];
[aNode setLocation:cc3v(posx,0,0)];
aNode.material = [CC3Material material];
[self addChild:aNode];
id move3d=[CC3MoveTo actionWithDuration:1 moveTo:cc3v(posx,0,100)];
id remove=[CCCallFuncND actionWithTarget:self selector:@selector(removeObj:)];
[aNode runAction:[CCSequence actions:move3d,remove,nil]];
}
but it doesn't work......can anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还准备好相机和灯光吗?
如果没有相机信息,很难说,但我认为你可能没有在看立方体(相机丢失或瞄准错误),或者立方体太大而你在里面。
尝试使用相机和灯光以及一些更适中的尺寸。
以下是在 cocos3d XCode 项目模板中对我有用的一些示例代码:
注意:
cocos3d 自述文件包含安装 XCode 项目模板的说明(如果您还没有安装的话)。
我用这些东西替换了模板中的所有“Hello World”对象代码(注意它是相同的相机和灯光代码/注释);你可以把它留下,但你的盒子前面会有一些文字。
为了清楚起见,将 CC3MeshNode 更改为 CC3BoxNode
用 cocos2d 辅助函数替换了 arc4random()
CCRANDOM_MINUS1_1() 只是为了便于阅读(并分享这个
cocos2d gem)。
注释掉了CCFuncCallND,因为我没有removeObj函数。希望你能做到。 ;)
希望有帮助。
Do you have a camera and light set up as well?
Without camera info it's a hard to tell, but I think you might either not be looking at the cube (camera missing or aimed wrong), or the cube is too big and you're inside it.
Try it with a camera and light, and some more modest sizes.
Here's some example code that worked for me in the cocos3d XCode project template:
Notes:
cocos3d readme has instructions to install the XCode project templates, if you haven't yet.
I replaced all the "Hello World" object code in the template with this stuff (note it's the same camera and light code/comments); you can leave it but you'll have some words in front of your box.
Changed CC3MeshNode to CC3BoxNode for clarity, but totally CC3MeshNode works too.
Replaced arc4random() with the cocos2d helper function
CCRANDOM_MINUS1_1() just for ease of reading (and to share this
cocos2d gem).
Commented out the CCFuncCallND because I didn't have a removeObj function. Hope you do. ;)
Hope that helps.
我还没有开始使用 cocos3d,但我在 stackoverflow 上找到了这段有效的代码
I have not started with cocos3d but I have found this code on stackoverflow that works
有一个 CC3BoxNode 类可以帮助您做到这一点。例如:
这将创建一个 2x2x2 的盒子,其原点位于其绝对中心。
希望有帮助。
There is a CC3BoxNode class to help you do that. Eg:
This would create a 2x2x2 box with its origin point in its absolute center.
Hope that helps.