Search Results

Search found 8 results on 1 pages for 'wayfarer'.

Page 1/1 | 1 

  • Why does my program crash when given negative values?

    - by Wayfarer
    Alright, I am very confused, so I hope you friends can help me out. I'm working on a project using Cocos2D, the most recent version (.99 RC 1). I make some player objects and some buttons to change the object's life. But the weird thing is, the code crashes when I try to change their life by -5. Or any negative value for that matter, besides -1. NSMutableArray *lifeButtons = [[NSMutableArray alloc] init]; CCTexture2D *buttonTexture = [[CCTextureCache sharedTextureCache] addImage:@"Button.png"]; LifeChangeButtons *button = nil; //top left button = [LifeChangeButtons lifeButton:buttonTexture ]; button.position = CGPointMake(50 , size.height - 30); [button buttonText:-5]; [lifeButtons addObject:button]; //top right button = [LifeChangeButtons lifeButton:buttonTexture ]; button.position = CGPointMake(size.width - 50 , size.height - 30); [button buttonText:1]; [lifeButtons addObject:button]; //bottom left button = [LifeChangeButtons lifeButton:buttonTexture ]; button.position = CGPointMake(50 , 30); [button buttonText:5]; [lifeButtons addObject:button]; //bottom right button = [LifeChangeButtons lifeButton:buttonTexture ]; button.position = CGPointMake(size.width - 50 , 30); [button buttonText:-1]; [lifeButtons addObject:button]; for (LifeChangeButtons *theButton in lifeButtons) { [self addChild:theButton]; } This is the code that makes the buttons. It simply makes 4 buttons, puts them in each corner of the screen (size is the screen) and adds their life change ability, 1,-1,5, or -5. It adds them to the array and then goes through the array at the end and adds all of them to the screen. This works fine. Here is my code for the button class: (header file) // // LifeChangeButtons.h // Coco2dTest2 // // Created by Ethan Mick on 3/14/10. // Copyright 2010 Wayfarer. All rights reserved. // #import "cocos2d.h" @interface LifeChangeButtons : CCSprite <CCTargetedTouchDelegate> { NSNumber *lifeChange; } @property (nonatomic, readonly) CGRect rect; @property (nonatomic, retain) NSNumber *lifeChange; + (id)lifeButton:(CCTexture2D *)texture; - (void)buttonText:(int)number; @end Implementation file: // // LifeChangeButtons.m // Coco2dTest2 // // Created by Ethan Mick on 3/14/10. // Copyright 2010 Wayfarer. All rights reserved. // #import "LifeChangeButtons.h" #import "cocos2d.h" #import "CustomCCNode.h" @implementation LifeChangeButtons @synthesize lifeChange; //Create the button +(id)lifeButton:(CCTexture2D *)texture { return [[[self alloc] initWithTexture:texture] autorelease]; } - (id)initWithTexture:(CCTexture2D *)atexture { if ((self = [super initWithTexture:atexture])) { //NSLog(@"wtf"); } return self; } //Set the text on the button - (void)buttonText:(int)number { lifeChange = [NSNumber numberWithInt:number]; NSString *text = [[NSString alloc] initWithFormat:@"%d", number]; CCLabel *label = [CCLabel labelWithString:text fontName:@"Times New Roman" fontSize:20]; label.position = CGPointMake(35, 20); [self addChild:label]; } - (CGRect)rect { CGSize s = [self.texture contentSize]; return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height); } - (BOOL)containsTouchLocation:(UITouch *)touch { return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]); } - (void)onEnter { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; [super onEnter]; } - (void)onExit { [[CCTouchDispatcher sharedDispatcher] removeDelegate:self]; [super onExit]; } - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint touchPoint = [touch locationInView:[touch view]]; touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint]; if ( ![self containsTouchLocation:touch] ) return NO; NSLog(@"Button touch event was called returning yes. "); //this is where we change the life to each selected player NSLog(@"Test1"); NSMutableArray *tempArray = [[[UIApplication sharedApplication] delegate] selectedPlayerObjects]; NSLog(@"Test2"); for (CustomCCNode *aPlayer in tempArray) { NSLog(@"we change the life by %d.", [lifeChange intValue]); [aPlayer changeLife:[lifeChange intValue]]; } NSLog(@"Test3"); return YES; } - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint touchPoint = [touch locationInView:[touch view]]; touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint]; NSLog(@"You moved in a button!"); } - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { NSLog(@"You touched up in a button"); } @end Now, This function: - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event Is where all the shit goes down. It works for all of the buttons except the -5 one. And then, it gets to: NSLog(@"we change the life by %d.", [lifeChange integerValue]); And it crashes at that statement. It only crashes when given anything less than -1. -1 works, but nothing smaller does. Here is the code in the CustomCCNode Class, "changeLife" that is being called. - (void)changeLife:(int)lifeChange { NSLog(@"change life in Custom Class was called"); NSLog(@"wtf is lifechange: %d", lifeChange); life += lifeChange; lifeString = [[NSString alloc] initWithFormat:@"%d",life]; [text setString:lifeString]; } Straight forward, but when the NSnumber is -5, it doesn't even get called, it crashes at the NSlog statement. So... what's up with that?

    Read the article

  • How to create a UIScrollView Programmatically?

    - by Wayfarer
    Alright, so the key here is I'm not using IB at all, because the View I'm working with is created programmatically. The UIView covers the lower half the screen, and has a bunch of buttons on it. However, I want to add more buttons to the UIView, without making it any larger. To do so, I want to make a UIScrollView inside the view, which will allow me to add more buttons off screen so the user can scroll to them. I think that's how it works. self.manaView = [[[UIView alloc] initWithFrame:frame] autorelease]; self.manaView.backgroundColor = [UIColor purpleColor]; UIScrollView *scroll = [UIScrollView alloc]; scroll.contentSize = CGSizeMake(320, 400); scroll.showsHorizontalScrollIndicator = YES; [self.manaView addSubview:scroll]; The first part of the code iniates my UIView, which works great, but I can't figure out how to make the UIScrollView programmatically and add it to the view, and then add the buttons to it. UIButton *ret2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; ret2.tag = 102; ret2.frame = CGRectMake(255, 5, 60, 50); [ret2 setTitle:@"Return" forState:UIControlStateNormal]; [ret2 addTarget:self action:@selector(flipAction:) forControlEvents:UIControlEventTouchUpInside]; [scroll addSubview:ret2]; When I did that, the button simply disappeared off my screen. So How do I do this correctly? Thank you for your help!

    Read the article

  • How to get objects to react to touches in Cocos2D?

    - by Wayfarer
    Alright, so I'm starting to learn more about Coco2D, but I'm kinda frusterated. A lot of the tutorials I have found are for outdated versions of the code, so when I look through and see how they do certain things, I can't translate it into my own program, because a lot has changed. With that being said, I am working in the latest version of Coco2d, version 0.99. What I want to do is create a sprite on the screen (Done) and then when I touch that sprite, I can have "something" happen. For now, let's just make an alert go off. Now, I got this code working with the help of a friend. Here is the header file: // When you import this file, you import all the cocos2d classes #import "cocos2d.h" // HelloWorld Layer @interface HelloWorld : CCLayer { CGRect spRect; } // returns a Scene that contains the HelloWorld as the only child +(id) scene; @end And here is the implementation file: // // cocos2d Hello World example // http://www.cocos2d-iphone.org // // Import the interfaces #import "HelloWorldScene.h" #import "CustomCCNode.h" // HelloWorld implementation @implementation HelloWorld +(id) scene { // 'scene' is an autorelease object. CCScene *scene = [CCScene node]; // 'layer' is an autorelease object. HelloWorld *layer = [HelloWorld node]; // add layer as a child to scene [scene addChild: layer]; // return the scene return scene; } // on "init" you need to initialize your instance -(id) init { // always call "super" init // Apple recommends to re-assign "self" with the "super" return value if( (self=[super init] )) { // create and initialize a Label CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Times New Roman" fontSize:64]; // ask director the the window size CGSize size = [[CCDirector sharedDirector] winSize]; // position the label on the center of the screen label.position = ccp( size.width /2 , size.height/2 ); // add the label as a child to this Layer [self addChild: label]; CCSprite *sp = [CCSprite spriteWithFile:@"test2.png"]; sp.position = ccp(300,200); [self addChild:sp]; float w = [sp contentSize].width; float h = [sp contentSize].height; CGPoint aPoint = CGPointMake([sp position].x - (w/2), [sp position].y - (h/2)); spRect = CGRectMake(aPoint.x, aPoint.y, w, h); CCSprite *sprite2 = [CCSprite spriteWithFile:@"test3.png"]; sprite2.position = ccp(100,100); [self addChild:sprite2]; //[self registerWithTouchDispatcher]; self.isTouchEnabled = YES; } return self; } // on "dealloc" you need to release all your retained objects - (void) dealloc { // in case you have something to dealloc, do it in this method // in this particular example nothing needs to be released. // cocos2d will automatically release all the children (Label) // don't forget to call "super dealloc" [super dealloc]; } - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; //CGPoint location = [[CCDirector sharedDirector] convertCoordinate:[touch locationInView:touch.view]]; CGPoint location = [touch locationInView:[touch view]]; location = [[CCDirector sharedDirector] convertToGL:location]; if (CGRectContainsPoint(spRect, location)) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Win" message:@"testing" delegate:nil cancelButtonTitle:@"okay" otherButtonTitles:nil]; [alert show]; [alert release]; NSLog(@"TOUCHES"); } NSLog(@"Touch got"); } However, this only works for 1 object, the sprite which I create the CGRect for. I can't do it for 2 sprites, which I was testing. So my question is this: How can I have all sprites on the screen react to the same event when touched? For my program, the same event needs to be run for all objects of the same type, so that should make it a tad easier. I tried making a subclass of CCNode and over write the method, but that just didn't work at all... so I'm doing something wrong. Help would be appreciated!

    Read the article

  • How to remove view from window?

    - by Wayfarer
    I am using Cocos2D for my main framework. In some cases, I want Cocos2D to load a nib file and have that be the view: window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; TargetPlayerViewController *myController = [[TargetPlayerViewController alloc]initWithNibName:@"TargetPlayerViewController" bundle:nil]; [window addSubview:[myController view]]; [window makeKeyAndVisible]; This works as expected, and shows the TargetPlayerViewController. Wonderful! What I need to know is: once that view has been loaded, how can I have the view remove itself? I've tried a few different ways, but all of them result in the program crashing. To test I have a button on the view set up which triggers this method: - (IBAction)GTFOnow:(id)sender { NSLog(@"GFTO"); window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //[self.view removeFromSuperview]; //[window makeKeyAndVisible]; } In this method the view should remove itself from the view so I can go back to the Cocos2D. How do I do this?

    Read the article

  • How to make a Global Array?

    - by Wayfarer
    So, I read this post, and it's pretty much exactly what I was looking for. However... it doesn't work. I guess I'm not going to go with the singleton object, but rather making the array in either a Global.h file, or insert it into the _Prefix file. Both times I do that though, I get the error: Expected specifier-qualifier-list before 'static' and it doesn't work. So... I'm not sure how to get it to work, I can remove extern and it works, but I feel like I need that to make it a constant. The end goal is to have this Mutable Array be accessible from any object or any file in my project. Help would be appreciated! This is the code for my Globals.h file: #import <Foundation/Foundation.h> @interface Globals : NSObject { static extern NSMutableArray * myGlobalArray; } @end I don't think I need anything in the implementation file. If I were to put that in the prefix file, the error was the same.

    Read the article

  • Giving another object a NSManagedObject

    - by Wayfarer
    Alright, so I'm running into an issue with my code. What I have done is subclassed UIButton so I can give it some more infomormation that pertain to my code. I have been able to create the buttons and they work great. Capiche. However, one of the things I want my subclass to hold is a reference to a NSMangedObject. I have this code in my header file: @interface ButtonSubclass : UIButton { NSManagedObjectContext *context; NSManagedObject *player; } @property (nonatomic, retain) NSManagedObject *player; @property (nonatomic, retain) NSManagedObjectContext *context; - (id)initWithFrame:(CGRect)frame andTitle:(NSString*)title; //- (void)setPlayer:(NSManagedObject *)aPlayer; @end As you can see, it has a instance variable to the NSMangedobject I want it to hold (as well as the Context). But for the life of me, I can't get it to hold that NSManagedObject. I run both the @synthesize methods in the Implementation file. @synthesize context; @synthesize player; So I'm not sure what I'm doing wrong. This is how I create my button: ButtonSubclass *playerButton = [[ButtonSubclass alloc] initWithFrame:frame andTitle:@"20"]; //works playerButton.context = self.context; //works playerButton.player = [players objectAtIndex:i]; //FAILS And I have initilaized the players array earlier, where I get the objects. Another weird thing is that when it gets to this spot in the code, the app crashes (woot) and the the console output stops. It doesn't give me any error, and notification at all that the app has crashed. It just... stops. So I don't even know what the error is that is crashing the code, besides it has to do with that line up there setting the "player" variable. Thoughts and ideas? I'd love your wisdom!

    Read the article

  • Losing NSManaged Objects in my Application

    - by Wayfarer
    I've been doing quite a bit of work on a fun little iPhone app. At one point, I get a bunch of player objects from my Persistant store, and then display them on the screen. I also have the options of adding new player objects (their just custom UIButtons) and removing selected players. However, I believe I'm running into some memory management issues, in that somehow the app is not saving which "players" are being displayed. Example: I have 4 players shown, I select them all and then delete them all. They all disappear. But if I exit and then reopen the application, they all are there again. As though they had never left. So somewhere in my code, they are not "really" getting removed. MagicApp201AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; context = [appDelegate managedObjectContext]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *desc = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:context]; [request setEntity:desc]; NSError *error; NSMutableArray *objects = [[[context executeFetchRequest:request error:&error] mutableCopy] autorelease]; if (objects == nil) { NSLog(@"Shit man, there was an error taking out the single player object when the view did load. ", error); } int j = 0; while (j < [objects count]) { if ([[[objects objectAtIndex:j] valueForKey:@"currentMultiPlayer"] boolValue] == NO) { [objects removeObjectAtIndex:j]; j--; } else { j++; } } [self setPlayers:objects]; //This is a must, it NEEDS to work Objects are all the players playing So in this snippit (in the viewdidLoad method), I grab the players out of the persistant store, and then remove the objects I don't want (those whose boolValue is NO), and the rest are kept. This works, I'm pretty sure. I think the issue is where I remove the players. Here is that code: NSLog(@"Remove players"); /** For each selected player: Unselect them (remove them from SelectedPlayers) Remove the button from the view Remove the button object from the array Remove the player from Players */ NSLog(@"Debugging Removal: %d", [selectedPlayers count]); for (int i=0; i < [selectedPlayers count]; i++) { NSManagedObject *rPlayer = [selectedPlayers objectAtIndex:i]; [rPlayer setValue:[NSNumber numberWithBool:NO] forKey:@"currentMultiPlayer"]; int index = [players indexOfObjectIdenticalTo:rPlayer]; //this is the index we need for (int j = (index + 1); j < [players count]; j++) { UIButton *tempButton = [playerButtons objectAtIndex:j]; tempButton.tag--; } NSError *error; if ([context hasChanges] && ![context save:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } UIButton *aButton = [playerButtons objectAtIndex:index]; [players removeObjectAtIndex:index]; [aButton removeFromSuperview]; [playerButtons removeObjectAtIndex:index]; } [selectedPlayers removeAllObjects]; NSError *error; if ([context hasChanges] && ![context save:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } NSLog(@"About to refresh YES"); [self refreshAllPlayers:YES]; The big part in the second code snippet is I set them to NO for currentMultiPlayer. NO NO NO NO NO, they should NOT come back when the view does load, NEVER ever ever. Not until I say so. No other relevant part of the code sets that to YES. Which makes me think... perhaps they aren't being saved. Perhaps that doesn't save, perhaps those objects aren't being managed anymore, and so they don't get saved in. Is there a lifetime (metaphorically) of NSManaged object? The Players array is the same I set in the "viewDidLoad" method, and SelectedPlayers holds players that are selected, references to NSManagedObjects. Does it have something to do with Removing them from the array? I'm so confused, some insight would be greatly appreciated!!

    Read the article

  • Load NSAttributedString from File

    - by Wayfarer
    Is there a way to have some sort of rich text in a file, and load the file into an NSAttributedString? I looked into loading a .rtf file into an NSAttributed String: NSString *filePathFooter = [[NSBundle mainBundle] pathForResource:kFooterFileName ofType:@"rtf"]; NSError *error = nil; NSAttributedString *string = [[NSAttributedString alloc] initWithFileURL:[NSURL URLWithString:filePathFooter] options:nil documentAttributes:NULL error:&error]; But this leaves me with: Error Domain=NSCocoaErrorDomain Code=258 "The operation couldn’t be completed. (Cocoa error 258.)" Is there a way to do this?

    Read the article

1