【问题标题】:can't use a UIKit code in Cocos2D无法在 Cocos2D 中使用 UIKit 代码
【发布时间】:2012-02-28 10:41:12
【问题描述】:

好吧,我用 UIKit 创建了一个游戏,但我遇到了一些性能问题,所以我决定使用 Cocos2D。我想使用这个代码一个 cocos2D,但我无法做到:

- (CGPoint)randomPointSquare {
CGRect frame = [[self view] frame];

//CGFloat rand_x = ((float)arc4random() / (float)UINT_MAX) * (float)_window.frame.size.width;

NSInteger side = arc4random() / (UINT_MAX/4);
CGFloat offset = 0;

switch(side) {
    case 0: /* top */
        offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.width;
        return CGPointMake(offset, -10);

    case 1: /* bottom */
        offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.width;
        return CGPointMake(offset, frame.size.height-150);

    case 2: /* left */
        offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.height;
        return CGPointMake(-10, offset);

    default:
    case 3: /* right */
        offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.height;
        return CGPointMake(frame.size.width+200, offset);
}
}
-(void) createNewImage {

UIImage * image = [UIImage imageNamed:@"abouffer_03.png"];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setCenter:[self randomPointSquare]];
}

有人可以尝试翻译此代码以在 cocos2D 中使用它。谢谢。对不起我的英语我是法国人:/

【问题讨论】:

  • 您似乎尝试添加具有随机中心点的图像。在 Cocos2D 中,你应该看看 CCSprite 类,记住 Cocos2D 中的坐标不是左上原点。

标签: iphone xcode uikit cocos2d-iphone


【解决方案1】:

您可能最好从模板创建一个新的 cocos2d 项目,然后根据需要将您的游戏逻辑插入到代码中,同时根据需要翻译视图组件。

例如,您的 UIImage 将改为 CCSprite。

但您最好将运动代码封装在包含 CCSprite 的对象中,这样您就可以重用该对象。

我首先从模板创建一个新的 cocos2d 项目并查看 hello world 示例。然后开始添加一个 CCSprite 并让它移动一点。那里有很多教程 - This for example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多