Using a custom Class

Discussion in 'iOS Programming' started by SRossi, Oct 31, 2011.

  1. SRossi macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #1
    Hey all,

    First I'd like to say sorry if this post doesn't have enough details, been a while since I posted a question here :).

    Currently I am writing a small (And very very simple) game for university coursework (I'm not looking for someone to write it for me I am just stuck at a point) and I am trying to use a custom class from within another class. Currently I think it has been initialised properly but it is not actually stepped into the init code when I do something like:

    Code:
    cube = [cube init];
    I don't have an alloc method just an init method just now, could this be the problem?

    Currently the class that is calling "cube" header is looking like:

    Code:
    #import "MainCube.h"
    
    @interface GameScene : AbstractScene {
        //float transY;
        //float transX;
        
        GLfloat scaleMatrix[16];
        GLfloat rotationMatrix[16];
        GLfloat translationMatrix[16];
        GLfloat projectionMatrix[16];
        
        int firstTime;
        
        float lastPinchDistance;
        float lastZoomDistance;
        CGPoint lastTouchPosition;
        
        //GLuint texture;
        
        MainCube *cube;
    }
    And I am trying to use it within this class like this:

    Code:
    - (void)initScene {
        cube = [cube init];
        GLint framebufferWidth, framebufferHeight;
        
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
        glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &framebufferHeight);
        
        GLfloat aspectRatio = (GLfloat)framebufferHeight / (GLfloat)framebufferWidth;
        
        lastZoomDistance = -5.0f;
        
        [EDMatrixTools applyIdentity:scaleMatrix];
        [EDMatrixTools applyIdentity:rotationMatrix];
        [EDMatrixTools applyIdentity:translationMatrix];
        [EDMatrixTools applyIdentity:projectionMatrix];
        
        [EDMatrixTools applyTranslation:translationMatrix x:0 y:0 z:lastZoomDistance];
        [EDMatrixTools applyProjection:projectionMatrix fov:45 aspect:aspectRatio near:1 far:100];
        
        [cube loadTexturesForScene];
    }
    I thought that all I would need to do is create a pointer to the class, initialise this pointer using the init call and then be able to use it with the loadTexturesForScene call, but currently it will not even go into the init method of the MainCube class.
    I have stepped through from the start till the application is running and it does not access the MainCube class at all.
    Another thing even after the
    Code:
    cube = [cube init];
    call in the class cube is (MainCube *) 0x0 which would be explained because it is not currently entering the init function at all.

    A few words of wisdom would be good as this is all that is stopping me from completing my coursework just now as I am trying to refactor it so that it runs slightly quicker :p

    Thanks all,
    Stephen
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You do not need your own alloc method (assuming you inherit from NSObject). But you must call alloc. Otherwise no memory space for your object will be created.
     
  3. SRossi thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #3
    Thanks for the quick reply, stupidly I had been doing the alloc command incorrect, although this has not helped, it still won't access the init method for some reason, there is no errors at all and it can see the method etc. but its as though every command I do with the MainCube class is just ignored completely :confused:.
     
  4. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #4
    When an object (GameScene) is created, all its instance variables/properties begin as nil / 0 / false, depending on type. This will be the value of the variable cube when you send init to it. In Objective-C, nil can receive method calls, it will just do nothing and will return nil / 0 / false, depending on what its return type is. This is what is happening. As robbieduncan says, you need to create the object with +alloc before it can receive -init.
     
  5. SRossi thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #5
    Thanks people, great help. I had completely forgot that it wouldn't be initialised and had changed it to alloc it after robbieduncan had said but placed the call in the wrong location. Put it in the original init of the GameScene class and all is well.

    Again thanks I can finally continue again :D
     

Share This Page