Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-29-2011, 02:12 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 17
jdunehew is on a distinguished road
Default Classes issue with game

I am working on a basic game in order to learn more about development.

I can create an instance of a class and set properties and what not with it inside a function but my problem is I want to do this:

Create an object of a monster (monsterA) in function monsterhandler().

When user presses a button like maybe a 'fight' button, it modifies monsterA.Health, but I would like to make this modification inside the function that my IBAction calls (lets say fighHandler() ).

How can I create the monsterA, but still be able to modify it's properties in another function that did not create it?

Thank you in advance.
jdunehew is offline   Reply With Quote
Old 12-29-2011, 06:44 AM   #2 (permalink)
Bringing fun to you
 
davidlxk's Avatar
 
Join Date: Dec 2011
Posts: 43
davidlxk is on a distinguished road
Default

you could pass the monster into the function as a parameter/argument and modify the monster's properties in the function
__________________
Drop Dem is out now! You should check it out at http://itunes.apple.com/us/app/drop-dem/id490101113

You should follow Drop Dem on Twitter http://twitter.com/dropdemgame
or find us on Facebook http://www.facebook.com/pages/Drop-Dem/314923981881703
davidlxk is offline   Reply With Quote
Old 01-07-2012, 04:51 PM   #3 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

In general, you use pointers to objects. You may create your monster object in game.startGame() or game.loadLevel() method, but you keep a pointer to it. Maybe you keep an array of pointers because you have many monsters.

To make changes to the monster you make a copy of the pointer and call methods on the pointer. This is different than making a copy of the object.

Code:
// fighthandler:
Monster* closeMonster = [game findCloseMonster];
if(closeMonster!=nil){
     [closeMonster subtractHealth:10];
}

The important thing here is that the pointer closeMonster is not a NEW monster object - it points to the same object returned by the flindCloseMonster method. If this doesn't make sense, read up on pointers.

EDIT: I just realized that you may not understand instance variables. Look up instance variables first - that's how you create a variable that you can use in two different methods.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-07-2012, 10:34 PM   #4 (permalink)
Bringing fun to you
 
davidlxk's Avatar
 
Join Date: Dec 2011
Posts: 43
davidlxk is on a distinguished road
Default

you should definitely check this link out for beginning obj c
iPhone Tutorials | Ray Wenderlich
__________________
Drop Dem is out now! You should check it out at http://itunes.apple.com/us/app/drop-dem/id490101113

You should follow Drop Dem on Twitter http://twitter.com/dropdemgame
or find us on Facebook http://www.facebook.com/pages/Drop-Dem/314923981881703
davidlxk is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 386
9 members and 377 guests
apatsufas, comicool, Creativ, dansparrow, husthlj, LunarMoon, mer10, Murphy, pbart
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,677
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, husthlj
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:04 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0