Problems with variables in Obj-C

Moderator
Posts: 1,140
Joined: 2005.07
Post: #16
I guess that would be it. I assumed he would be putting it directly nextTileNum, but I should have figured that since he's new, he probably won't think of things like that. Annoyed
Quote this message in a reply
Member
Posts: 29
Joined: 2006.07
Post: #17
Wow, major changes. Everything now runs great with no errors, though there are still plenty of things to clean up and probably a few remaining memory leaks. I split the variable and object initializations to the header file and supplied init and dealloc routines. I also enabled it to use full coordinates and paint the entire view with tiles from 'mapl'. I'm very satisfied. Thanks for all the help getting me here!

The complete MyView.m file:

Code:
#import "MyView.h"

@implementation MyView

- (id)initWithFrame:(NSRect)frameRect
{
    if ((self = [super initWithFrame:frameRect]) != nil) {
        // Load tile images from the application's bundle
        tile01 = [[NSImage imageNamed:@"T01"] retain];
        tile02 = [[NSImage imageNamed:@"T02"] retain];
        tile03 = [[NSImage imageNamed:@"T03"] retain];
        
        // Actual map data (background tiles)
        mapl   = @"21111111111221111131111221111311111221111111111221111111111221111131111221111311​1112211111111112211111111312222222222222";
    }
    return self;
}

- (void)drawRect:(NSRect)rect
{    
    // Start drawing from (0,0)
    drawX        = 0;
    drawY        = 0;

    // Set the bounds of the view
    bounds = [self bounds];
    
    // Set the background color to black
    [[NSColor blackColor] set];
    [NSBezierPath fillRect:bounds];

    // Draw the tiles in mapl
    for ( curTile = 0; curTile < [mapl length]; curTile++ ) {
        // Capture tile number of current tile in the index (mapl)
        nextTile = [mapl substringWithRange:NSMakeRange(curTile,1)];
        nextTileNum = [nextTile intValue];
        
        // Move to the next row of tiles (Y) when all tiles
        // in the current row have been drawn
        if (drawX == 12)
            [self shiftDrawingRow];

        // Draw the tile that corresponds to the current coordinate
        switch (nextTileNum) {
            case 1:
                tilePoint.x = (drawX * 16);
                tilePoint.y = (144 - (drawY * 16));
                [tile01 dissolveToPoint:tilePoint fraction:1.0];
                break;
            case 2:
                tilePoint.x = (drawX * 16);
                tilePoint.y = (144 - (drawY * 16));
                [tile02 dissolveToPoint:tilePoint fraction:1.0];
                break;
            case 3:
                tilePoint.x = (drawX * 16);
                tilePoint.y = (144 - (drawY * 16));
                [tile03 dissolveToPoint:tilePoint fraction:1.0];
                break;
        }
        // Increment X value by one for next tile
        drawX++;
    }
}

-(void) shiftDrawingRow
{
    // Start drawing the next row of tiles
    drawX = 0;
    drawY++;
}

-(void) dealloc
{
    [tile01 release];
    [tile02 release];
    [tile03 release];
    [nextTile release];
    
    [super dealloc];
}
@end

[Image: mygame.jpg]

Up next : use an array to keep track of images instead of a switch statement and clean up memory management. Also note that I'm still using substringWithRange: but hey, it works for now. I'll try better alternatives later.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  C: Global Variables versus Parameters Lizard Man 10 6,493 Jan 13, 2010 08:22 PM
Last Post: Lizard Man
  Accessing an inherited class's variables Tobs_ 22 10,107 Feb 28, 2007 05:26 PM
Last Post: mac_girl
  Should global variables be pointers or full objects? ia3n_g 1 2,501 Aug 4, 2006 05:53 PM
Last Post: OneSadCookie
  where do global variables fall into the memory type? WhatMeWorry 3 2,980 Jun 5, 2006 02:45 PM
Last Post: OneSadCookie
  Arrays or variables containing executable functions Jones 4 4,887 Jun 2, 2006 08:35 AM
Last Post: Zekaric