开发者

Cocoa: Passing texture atlas values to a NSView

I'm passing values to a method in a NSView subclass - I'm calling it twice but the drawRect method is only showing the last value passed...the values are coordinates on a texture atlas, not sure if that is clear so here's my code:

in my AppController.m:

- (IBAction)setCards:(id)sender {

    //get window frame for positioning
    NSRect winFrame  = [[NSApp mainWindow] frame];

    //add back shields
    float backArrowX = (winFrame.size.width/2)-90;
    float backArrowY = (winFrame.size.height/2 - 180.0);

    //set view in window location
    Shield* backShield = [[Shield alloc] initWithFrame:NSMakeRect(backArrowX, backArrowY, 50.0, 50.0)];

    //this passes coordinates for the texture atlas to the Shield class
    [backShield setShield:100.0 :1050.0 :50.0 :50.0];

    //display view in window
    [[[NSApp mainWindow] contentView]addSubview:backShield];

    //add forward shield (same process as above, the x coordinate changes
    float forwardArrowX = winFrame.size.width/2+40;
    float forwardArrowY = (winFrame.size.height/2 - 180.0);

    Shield* frontShield = [[Shield alloc] initWithFrame:NSMakeRect(forwardArrowX, forwardArrowY, 50.0, 50.0)];
    [frontShield setShield:150.0 :1050.0 :50.0 :50.0];

    [[[NSApp mainWindow] contentView]addSubview:frontShield];

    //release stuff we creat开发者_JAVA百科ed
    [backShield release];
    [frontShield release];
}

in my Shield.h file:

#import <Cocoa/Cocoa.h>

float shieldFrameX;
float shieldFrameY;
float shieldFrameW; 
float shieldFrameH; 

@interface Shield : NSView {

}

-(void) setShield : (float) sX : (float) sY : (float) sW : (float) sH; 
@end

in Shield.m

- (void)drawRect:(NSRect)rect {

    NSLog(@"%f", shieldFrameX);

    NSImage *imageFromBundle = [NSImage imageNamed:@"gameicons.png"];
    [self setNeedsDisplay:YES];
    [imageFromBundle drawInRect:[self bounds] fromRect:NSMakeRect(shieldFrameX, shieldFrameY, shieldFrameW, shieldFrameH) operation: NSCompositeCopy fraction:1.0];

}

-(void) setShield : (float) sX : (float) sY : (float) sW : (float) sH { 

    shieldFrameX = sX;
    shieldFrameY = sY; 
    shieldFrameW = sW;
    shieldFrameH = sH;

}

What I am expecting to happen is that the Shield class is called twice, each time the setShield method is passed a set of floats that designate the x,y and w.h location of an image on my texture atlas. Then drawRect will use those coordinates (in the drawInRect) to pull the correct image from the texture atlas.

What is happening is if I log sX or shieldFrameX in the setShield method I get the correct results, in this case 100, 150. However, if I log shieldFrameX in the drawRect method I get the same results, 150,150. How can that be? When you instantiate a class isn't it independent of any other instantiation of that class? So whatever values I send to class A wouldn't be reflected in class B, right?

What I am getting are two of the same images in different locations in my window.


You've made the value variables in Shield:

float shieldFrameX;
float shieldFrameY;
float shieldFrameW; 
float shieldFrameH; 

essentially "class" variables because there will only be one instance of them. Make them instance variables by putting them inside the declaration like so:

@interface Shield : NSView {
    float shieldFrameX;
    float shieldFrameY;
    float shieldFrameW; 
    float shieldFrameH; 
}

That way there will be a copy of these variables for each instance of the class and thus they can be different for each instance of the class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜