Topic:   My iPhone Game Tutorials for Beginners   (Read 423188 times)


0 Members and 1 Guest are viewing this topic.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #240 on: May 20, 2011, 08:40:49 AM »
Here's an example of a delegate .h:
Code: [Select]
#import <UIKit/UIKit.h>

@interface AppDelegate : NSObject <UIApplicationDelegate> {
    NSString* name;
    BOOL soundOption;
}

@property (nonatomic, retain) NSString* name;
@property (nonatomic) BOOL soundOption;

@end

Top of delegate .m file:
Code: [Select]
#import "AppDelegate.h"

@implementation QAppDelegate
@synthesize name,soundOption;

That makes 2 global variables, name and soundOption.
To use them in other classes you put this in the top of the .m:
Code: [Select]
#import "AppDelegate.h" 
AppDelegate *delegate;
And this in the loading of that class:
Code: [Select]
delegate = [[UIApplication sharedApplication] delegate]; 

Then in that class you can access those variable by delegate.soundOption or delegate.name.

breshi


  • GMG Newbie

  • *


  • Posts: 31

  • Carpe Diem
Re: My iPhone Game Tutorials for Beginners
« Reply #241 on: May 23, 2011, 08:15:46 PM »
Sorry for the delay in replying but I have only just had a chance this morning to get back to the code.

I have put in the code you suggested but in the AppDelegate.m file I have put in the line:
@synthesize soundOption;
and get this error:
Type of property 'soundOption ('signed char') does not match type of var of ivar 'soundOption' ('_Bool')

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #242 on: May 23, 2011, 08:34:42 PM »
Can you paste the code you used?

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: My iPhone Game Tutorials for Beginners
« Reply #243 on: May 23, 2011, 08:42:45 PM »
what commands would you use to create and control a SPRITE, and make grid nave. I'm trying to make an iPhone RPG!
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #244 on: May 23, 2011, 08:59:11 PM »
One of the easiest ways would be to make UIImageViews as the sprites. Then just move 'em around.

breshi


  • GMG Newbie

  • *


  • Posts: 31

  • Carpe Diem
Re: My iPhone Game Tutorials for Beginners
« Reply #245 on: May 23, 2011, 09:59:19 PM »
In the AppDelegate.h I have:

#import <UIKit/UIKit.h>
#import "MainMenu.h"

@interface AppDelegate : NSObject <UIApplicationDelegate> {
    IBOutlet UINavigationController* nav;
    IBOutlet MainMenu* mainMenu;
        
bool accelerometerOption;
bool vibOption;
bool soundOption;
    bool musicOption;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic) BOOL accelerometerOption;
@property (nonatomic) BOOL vibOption;
@property (nonatomic) BOOL soundOption;
@property (nonatomic) BOOL musicOption;

@end

and in the appDelegate.m I have:



#import "AppDelegate.h"

@implementation AppDelegate
@synthesize window;
@synthesize accelerometerOption;
@synthesize vibOption;
@synthesize soundOption;
@synthesize musicOption;


All 4 synthesize commands show a similar error message.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #246 on: May 23, 2011, 10:36:19 PM »
Maybe it's because you're having them as "bool" in your first declaration then as "BOOL" in your property declarations.

breshi


  • GMG Newbie

  • *


  • Posts: 31

  • Carpe Diem
Re: My iPhone Game Tutorials for Beginners
« Reply #247 on: May 23, 2011, 10:49:53 PM »
That was it!!
I can't believe that it didn't like that.
Thanks.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #248 on: May 23, 2011, 11:03:21 PM »
Yeah, Obj-C is very case sensitive. One ill-placed capital can ruin the whole program.

breshi


  • GMG Newbie

  • *


  • Posts: 31

  • Carpe Diem
Re: My iPhone Game Tutorials for Beginners
« Reply #249 on: May 23, 2011, 11:12:26 PM »
OK did all that and put the relevant code in the AppDelegate.h and .m files.
Thus creating 4 global bool variables.

In the MainMenu.m, the OptionsScreen.m and the GameScreen.m I put this at the top:
#import "AppDelegate.h"  
AppDelegate *delegate;
and this in the ViewDidLoad class:
delegate = [[UIApplication sharedApplication] delegate];

I had to redeclare these four variables in each of the .h files as I was getting error messages with out them there- "Use of undeclared identifier 'soundOption'"

I set them all as TRUE in the MainMenu.m file but they do not flow through.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #250 on: May 23, 2011, 11:19:30 PM »
Don't re-declare your global vars in your other classes. Just in the AppDelegate.

To access the global vars you gotta do:
delegate.soundOption

breshi


  • GMG Newbie

  • *


  • Posts: 31

  • Carpe Diem
Re: My iPhone Game Tutorials for Beginners
« Reply #251 on: May 23, 2011, 11:50:27 PM »
I've said it before, and I will say it again - You are a legend!!
Thank you.

Now it has brought to light one other question.
In my optionsScreen I have four toggle switches which enables me to change my options but they do not display properly.
How can you set them in the program? and
Where do you set them in the program as if you do it in the ViewDidLoad isn't that too late?

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #252 on: May 23, 2011, 11:56:35 PM »
You can set them in viewDidLoad or viewDidAppear. Neither are too late.
To figure out what code to use I'd suggest going to the Developer Documentation in Xcode and searching the toggle switch control. When at the docs for the switch you can scroll down to see all the functions for it and definitions of what they do. Very handy.

But the functions is probably somewhere along the lines of setState:
« Last Edit: May 23, 2011, 11:57:59 PM by Gandolf »

breshi


  • GMG Newbie

  • *


  • Posts: 31

  • Carpe Diem
Re: My iPhone Game Tutorials for Beginners
« Reply #253 on: May 24, 2011, 12:24:12 AM »
Where would you put something you only want run the very first time you run your template.
I want to set my options once and then let the user change them with out getting them reset.
As the template goes back and fourth between different screens including MainMenu where would I find a class that only ever gets run once when you start the app?

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #254 on: May 24, 2011, 12:26:48 AM »
That would be the delegate. On application load in the app delegate. Ran once. When the app opens.