Bizarre glitch...
I'm developing a game that's making very nice progress. But out of nowhere, I've been getting an odd graphic glitch. In my init routine, I initialize my sprites in order as follows:
...other inits
SPMR[2] = [[NSImage imageNamed:@"CR2"] retain];
playerFace1 = [[NSImage imageNamed:@"PLRFACE1"] retain];
...other inits
For some reason (and no other images have an issue with this), the image for playerFace1 shows up for SPMR[2] instead of the normal image. playerFace1 displays just fine as what it's supposed to be. I tried deleting the declaration for SPMR[2] and declaring it later in the init routine, thinking that may fix it, but then playerFace1 starts showing the image for SPMR[2] (which still didn't show it's correct image file). I've checked that all the necessary image files are in the right place and the right name...doesn't make a difference. What's going on?
...other inits
SPMR[2] = [[NSImage imageNamed:@"CR2"] retain];
playerFace1 = [[NSImage imageNamed:@"PLRFACE1"] retain];
...other inits
For some reason (and no other images have an issue with this), the image for playerFace1 shows up for SPMR[2] instead of the normal image. playerFace1 displays just fine as what it's supposed to be. I tried deleting the declaration for SPMR[2] and declaring it later in the init routine, thinking that may fix it, but then playerFace1 starts showing the image for SPMR[2] (which still didn't show it's correct image file). I've checked that all the necessary image files are in the right place and the right name...doesn't make a difference. What's going on?
paste more code
Sir, e^iπ + 1 = 0, hence God exists; reply!
The full header with all declarations:
The full Init routine:
Code:
/* MyView */
#import <Cocoa/Cocoa.h>
@interface MyView : NSView
{
int gamestate;
int gameSubstate;
int cursorPos;
int cursorPos2;
int curMusic;
int intConv;
int i;
int tileX;
int tileY;
int charX;
int charY;
int charDirection;
int newX;
int newY;
int canPass;
int faderFrame;
int faderState;
int curWalkingFrame;
int animWalkingMod;
int battleValue;
int mapMusic;
int player1MaxHP;
int player1HP;
int player1MaxMP;
int player1MP;
int player1ATK;
int player1DEF;
int player1AGL;
int player1Weapon;
int player1Shield;
int player1Armor;
int player1Spell1;
int player1Spell2;
int player1Spell3;
int player1Skill;
int player1Level;
int player2MaxHP;
int player2HP;
int player2MaxMP;
int player2MP;
int player2ATK;
int player2DEF;
int player2AGL;
int player2Weapon;
int player2Shield;
int player2Armor;
int player2Spell1;
int player2Spell2;
int player2Spell3;
int player2Skill;
int player2Level;
int playerMoney;
int playerItem[20];
int curBattleDefNum;
int curNumEnemies;
int enemy1ID;
int enemy2ID;
int enemy3ID;
int enemy4ID;
int curBattleMods;
int curBattleMusic;
int curBattleMap;
NSRect viewRect;
NSImage *T[99];
NSImage *selImage;
NSImage *selImage2;
NSImage *BG[2];
NSImage *SPMU[2];
NSImage *SPMD[2];
NSImage *SPML[2];
NSImage *SPMR[2];
NSImage *SPU[3];
NSImage *SPD[3];
NSImage *SPL[3];
NSImage *SPR[3];
NSImage *playerFace1;
NSImage *playerFace2;
NSImage *menuMain;
NSImage *itemMain;
NSImage *battleStatsBar;
NSImage *faderLayer;
NSImage *faderLayerWhite;
NSImage *OTLogoScreen;
NSSound *mus[3];
NSSound *sfx[4];
NSString *strToWrite;
NSString *mapf; // Map flags
NSString *mapl; // Tiles
NSString *mapp; // Collision
NSString *mapm; // Index
NSString *smapf[5];
NSString *smapl[5];
NSString *smapp[5];
NSString *smapm[5];
NSString *nextTileContents;
NSString *animDef[1];
NSString *player1Name;
NSString *player2Name;
NSString *tempVar;
NSString *battleDef[2];
NSString *battleMap[2];
NSString *enemyDef[2];
NSMutableDictionary *largeTextAttribs;
NSMutableDictionary *smallTextAttribs;
NSMutableDictionary *tinyTextAttribs;
NSMutableDictionary *tinyBlackTextAttribs;
NSTimer *animTimer;
}
- (void)animTimer:(NSTimer *)theTimer;
- (BOOL)acceptsFirstResponder;
- (void)checkForCollision;
- (void)parseMapHeader;
@endThe full Init routine:
Code:
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil) {
T[0] = [[NSImage imageNamed:@"T00"] retain];
T[1] = [[NSImage imageNamed:@"T01"] retain];
T[2] = [[NSImage imageNamed:@"T02"] retain];
T[3] = [[NSImage imageNamed:@"T03"] retain];
T[4] = [[NSImage imageNamed:@"T04"] retain];
T[5] = [[NSImage imageNamed:@"T05"] retain];
T[6] = [[NSImage imageNamed:@"T06"] retain];
T[7] = [[NSImage imageNamed:@"T07"] retain];
T[8] = [[NSImage imageNamed:@"T08"] retain];
T[9] = [[NSImage imageNamed:@"T09"] retain];
T[10] = [[NSImage imageNamed:@"T10"] retain];
T[11] = [[NSImage imageNamed:@"T11"] retain];
T[12] = [[NSImage imageNamed:@"T12"] retain];
T[13] = [[NSImage imageNamed:@"T13"] retain];
T[14] = [[NSImage imageNamed:@"T14"] retain];
selImage = [[NSImage imageNamed:@"SEL"] retain];
selImage2 = [[NSImage imageNamed:@"SEL2"] retain];
BG[1] = [[NSImage imageNamed:@"BG1"] retain];
SPMU[1] = [[NSImage imageNamed:@"AU1"] retain];
SPMD[1] = [[NSImage imageNamed:@"AD1"] retain];
SPML[1] = [[NSImage imageNamed:@"AL1"] retain];
SPMR[1] = [[NSImage imageNamed:@"AR1"] retain];
SPMU[2] = [[NSImage imageNamed:@"AU2"] retain];
SPMD[2] = [[NSImage imageNamed:@"AD2"] retain];
SPML[2] = [[NSImage imageNamed:@"AL2"] retain];
SPMR[2] = [[NSImage imageNamed:@"AR2"] retain];
SPU[0] = [[NSImage imageNamed:@"BU1"] retain];
SPD[0] = [[NSImage imageNamed:@"BD1"] retain];
SPL[0] = [[NSImage imageNamed:@"BL1"] retain];
SPR[0] = [[NSImage imageNamed:@"BR1"] retain];
playerFace1 = [[NSImage imageNamed:@"PLRFACE1"] retain];
playerFace2 = [[NSImage imageNamed:@"PLRFACE2"] retain];
menuMain = [[NSImage imageNamed:@"mainmenu"] retain];
itemMain = [[NSImage imageNamed:@"itemmenu"] retain];
battleStatsBar = [[NSImage imageNamed:@"statsbar"] retain];
faderLayer = [[NSImage imageNamed:@"FADER"] retain];
faderLayerWhite = [[NSImage imageNamed:@"FADER2"] retain];
OTLogoScreen = [[NSImage imageNamed:@"otlogo"] retain];
mus[0] = [[NSSound soundNamed:@"MUS01"] retain];
mus[1] = [[NSSound soundNamed:@"MUS02"] retain];
mus[2] = [[NSSound soundNamed:@"MUS03"] retain];
sfx[0] = [[NSSound soundNamed:@"SFX00"] retain];
sfx[1] = [[NSSound soundNamed:@"SFX01"] retain];
sfx[2] = [[NSSound soundNamed:@"SFX02"] retain];
sfx[3] = [[NSSound soundNamed:@"SFX03"] retain];
largeTextAttribs = [[NSMutableDictionary dictionary] retain];
[largeTextAttribs setObject:[NSFont fontWithName:@"Charcoal" size:11]
forKey:NSFontAttributeName];
[largeTextAttribs setObject:[NSColor blackColor]
forKey:NSForegroundColorAttributeName];
smallTextAttribs = [[NSMutableDictionary dictionary] retain];
[smallTextAttribs setObject:[NSFont fontWithName:@"Charcoal" size:10]
forKey:NSFontAttributeName];
[smallTextAttribs setObject:[NSColor whiteColor]
forKey:NSForegroundColorAttributeName];
tinyTextAttribs = [[NSMutableDictionary dictionary] retain];
[tinyTextAttribs setObject:[NSFont fontWithName:@"04b03" size:8]
forKey:NSFontAttributeName];
[tinyTextAttribs setObject:[NSColor whiteColor]
forKey:NSForegroundColorAttributeName];
tinyBlackTextAttribs = [[NSMutableDictionary dictionary] retain];
[tinyBlackTextAttribs setObject:[NSFont fontWithName:@"04b03" size:8]
forKey:NSFontAttributeName];
[tinyBlackTextAttribs setObject:[NSColor blackColor]
forKey:NSForegroundColorAttributeName];
animTimer = [[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(animTimer:) userInfo:nil repeats:YES] retain];
smapf[0] = @"010010000000000";
smapl[0] = @"00010101111201010100000202021314020202000003030309100303030000040404050604040400000404040506040404000004040405060404040000040404050604040400000404040506040404000004040405060404040000000000070800000000";
smapp[0] = @"0000110000000011000000001100000000110000000011000000001100000000110000000011000000001100000000110000";
smapm[0] = @"01000000";
smapf[1] = @"010010000000000";
smapl[1] = @"01010101010101010101010000000000000000010100000000000000000101000000000000000001010000000000000000010100000000000000000101000000000000000001010000000000000000010100000000000000000101000000000000000001";
smapp[1] = @"0000000000011111111001111111100111111110011111111001111111100111111110011111111001111111100111111110";
smapm[1] = @"00000000";
mapf = smapf[0]; mapl = smapl[0];
mapp = smapp[0]; mapm = smapm[0];
battleDef[0] = @"101NNNNNN00200";
battleMap[0] = @"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
enemyDef[0] = @"0";
player1Name = @"Cecil"; player2Name = @"Rydia";
player1MaxHP = 250; player2MaxHP = 200;
player1HP = player1MaxHP; player2HP = player2MaxHP;
player1MaxMP = 10; player2MaxMP = 50;
player1MP = player1MaxMP; player2MP = player2MaxMP;
player1ATK = 10; player2ATK = 5;
player1DEF = 5; player2DEF = 7;
player1AGL = 3; player2AGL = 10;
player1Weapon = 0; player2Weapon = 0;
player1Shield = 0; player2Shield = 0;
player1Armor = 0; player2Armor = 0;
player1Spell1 = 0; player2Spell1 = 0;
player1Spell2 = 0; player2Spell2 = 0;
player1Spell3 = 0; player2Spell3 = 0;
player1Skill = 0; player2Skill = 0;
player1Level = 1; player2Level = 1;
playerMoney = 200;
playerItem[0] = 1; playerItem[10] = 1;
playerItem[1] = 1; playerItem[11] = 0;
playerItem[2] = 1; playerItem[12] = 0;
playerItem[3] = 1; playerItem[13] = 0;
playerItem[4] = 1; playerItem[14] = 0;
playerItem[5] = 1; playerItem[15] = 0;
playerItem[6] = 1; playerItem[16] = 0;
playerItem[7] = 1; playerItem[17] = 0;
playerItem[8] = 1; playerItem[18] = 0;
playerItem[9] = 2; playerItem[19] = 0;
gamestate = 1;
gameSubstate = 0;
cursorPos = 0;
curMusic = 1;
charX = 5;
charY = 4;
charDirection = 1;
faderFrame = 0;
faderState = 2;
curWalkingFrame = 0;
animWalkingMod = 0;
battleValue = 0;
viewRect.size.width = 80;
viewRect.size.height = 80;
viewRect.origin.x = 0;
viewRect.origin.y = 0;
//[self setBounds:viewRect];
}
return self;
}
Since SPMR is declared as NSImage * SPMR[2], accessing the element at index 2 is one beyond the end of the array, so you're overwriting memory somewhere past the address range of that variable.
Ahh, so basically, declaring variableName[2] creates variableName[0] and variableName[1]? I'm used to other languages where items 0-2 would be created. Thanks everyone, I feel stupid now
I feel sort of bad asking this, but do you know loops yet? You can simplify so much of that repetitive code by using a basic loop. Also, you can define variables like this:
Code:
int a, b, c, d, e, f;imikedaman Wrote:Also, you can define variables like this:
Code:
int a, b, c, d, e, f;
Only if you are a masochist or don't expect other people to understand your code...
I think he was meaning you can define variables
as opposed to
not, you can use single letter variable names
Code:
int a, b, c;Code:
int a;
int b;
int c;not, you can use single letter variable names
Sir, e^iπ + 1 = 0, hence God exists; reply!
Yes, I use loops to do a lot of my drawing and parsing code.
Each image is statically named, for example T01.gif corresponds to Tile[1]. How would I incorporate the index variable (in this case i) into the name of the file to load them using a loop?
something like ( imageNamed:@"T%d", i ) ?
Each image is statically named, for example T01.gif corresponds to Tile[1]. How would I incorporate the index variable (in this case i) into the name of the file to load them using a loop?
something like ( imageNamed:@"T%d", i ) ?
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Bizarre Bug | ferum | 7 | 3,326 |
Jan 20, 2007 03:26 PM Last Post: ferum |
|
| Bizarre Conditional Operator Problem in C++ | kingofsquirrels | 2 | 1,975 |
May 23, 2006 02:56 PM Last Post: kingofsquirrels |
|

