Austin
Austin

Reputation: 4929

Drawing a Rectangle with Cocos2d

So I'm trying to draw a simple rectangle in my iOS cocos2d game, but it's simply not showing up. At first, I added my rectangle drawing code to my Main scene's init method, but it wasn't showing up, so I looked around.

I read this http://www.cocos2d-iphone.org/forum/topic/655 and what I got from it, was to make a new class, extend the CCLayer class, and add it to my main scene.

This is my Main scene's code:

+(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;
}

And this is my RectLayer.m code,

#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

Now I'm getting a EXC_BAD_ACCESS error when trying to open it with the iPad simulator.

I've recently just started Objective-C, so I'm not quite sure what's wrong.

Thanks for any help.

Upvotes: 2

Views: 6457

Answers (2)

Rafael Martins Verri
Rafael Martins Verri

Reputation: 21

You also could put this code in method draw:

ccDrawSolidRect(CGPoint origin, CGPoint destination, ccColor4F color);

Upvotes: 0

sch
sch

Reputation: 27536

Put the drawing code in the method 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);
}

Upvotes: 4

Related Questions