ios - 用Cocos2d画一个矩形

标签 ios cocos2d-iphone

所以我试图在我的 iOS cocos2d 游戏中绘制一个简单的矩形,但它根本没有显示出来。首先,我将矩形绘制代码添加到主场景的 init 方法中,但它没有显示,所以我环顾四周。

我读了这个http://www.cocos2d-iphone.org/forum/topic/655我从中得到的是创建一个新类,扩展 CCLayer 类,并将其添加到我的主场景中。

这是我的主场景的代码:

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];
    RectLayer *rectLayer = [RectLayer node];
    // add layer as a child to scene
    [scene addChild:rectLayer];
    [scene addChild: layer];

    // return the scene
    return scene;
}

这是我的 RectLayer.m 代码,

#import "RectLayer.h"

@implementation RectLayer

-(id) init {
    if( (self=[super init] )) {
        glEnable(GL_LINE_SMOOTH);
        glColor4ub(255, 255, 255, 255);
        glLineWidth(2);
        CGPoint vertices2[] = { ccp(79,299), ccp(134,299), ccp(134,229), ccp(79,229) };
        ccDrawPoly(vertices2, 4, YES);
    }
}
@end

现在,当我尝试使用 iPad 模拟器打开它时,出现 EXC_BAD_ACCESS 错误。

我最近刚刚开始使用 Objective-C,所以我不太确定出了什么问题。

感谢您的帮助。

最佳答案

将绘图代码放入方法draw中:

- (void)draw
{
    glEnable(GL_LINE_SMOOTH);
    glColor4ub(255, 255, 255, 255);
    glLineWidth(2);
    CGPoint vertices2[] = { ccp(79,299), ccp(134,299), ccp(134,229), ccp(79,229) };
    ccDrawPoly(vertices2, 4, YES);
}

关于ios - 用Cocos2d画一个矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9442143/

相关文章:

ios - 多个属性的相同 didSet block

ios - 太多的 box2d 对象使我的游戏变慢

iphone - 如何在 Cocos2d CCLayer 中添加摇动手势?

ios - "How To Make a Tile-Based Game with Cocos2D 2.X"使用cocos2d V3制作本教程

iphone - XCode- "Previous frame inner to this frame (gdb could not unwind past this frame)"

iphone - 如何在 iOS 上阅读短信?

ios - 为什么百分比 UILabel 显示 'nan%'

ios - FFMpeg hls_time 精度

ios - 如何在 uitableview 中动态创建部分

iPhone游戏加载/保存/配置关卡