Cocos2D scoring game [duplicate] - objective-c

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Scoring System In Cocos2D
I got a reply from a question I asked earlier but I am new to coding and have no idea how to do it. Here is the reply:
"#synthesize a "score" property of type int, and a "scoreLabel" property of type CCLabelTTF.
initialize your score property to "0" in -(void)init
On line 126, increment your "score" property by 1, and set that value into your CCLabelTTF."
Can you tell me how to do this? plz. link to my other post
----- Scoring System In Cocos2D

When you synthesize a private variable (other classes cannot see it) you allow a way for other classes to see and/or modify the value of that variable.
First, you want to create the variable:
NSMutableArray *_targets;
NSMutableArray *_projectiles;
int _score;
CCLabelTTF *_scoreLabel;
Then in your init method to set the _score to 0:
-(id) init
{
if( (self=[super init] )) {
[self schedule:#selector(update:)];
_score = 0;
Then increment (add 1 to) your _score variable and set the string (the text content) of your _scoreLabel to that value.
if (CGRectIntersectsRect(projectileRect, targetRect)) {
[targetsToDelete addObject:target];
_score++;
[_scoreLabel setString:[NSString stringWithFormat:#"%d", _score]];
}
The line [_scoreLabel setString:[NSString stringWithFormat:#"%d", _score]]; is a way to convert the integer of _score to a string (NSString). It's an old C way of doing it, the %d means that whatever is going to be there should be displayed as an integer as opposed to a float (having decimal points).
It also looks like you need to "instantiate" your label and add it as a child to the layer. Instantiation is just a fancy term for creating a instance of something. Think of a "class" as a blueprint for a chair, and an "instance" as a chair created from that blueprint. Once you have the chair created (an instance), you can modify it (paint it, add/remove legs, etc).
So, to instantiate your label and add it to the layer (itself):
-(id) init
{
if( (self=[super init] )) {
[self schedule:#selector(update:)];
_score = 0;
//Create label
_scoreLabel = [CCLabelTTF labelWithString:#"0" fontName:#"Marker Felt" fontSize:16];
//Add it to a layer (itself)
[self addChild:_scoreLabel];

Create a score property in HelloWorldLayer.h after the interface declaration, like
#property (nonatomic, retain) int score;
Then synthesize it in your .m file just after the #implementation HelloWorldLayer line.
Create methods for setting and getting scores:
-(int)getScore {
return self.score;
}
-(void)setScore:(int)newScore {
self.score = newScore;
}
In the init method, set the value of the property to zero,
if( (self=[super init] )) {
//... other stuff
[self setScore:0]
}
You can update the score with the setScore method, but I suggest having another method for this that calls setScore so that you can use it at different places with a single line call, and make any changes like assigning more score in certain situations, like two collisions within half-a-second etc..
-(void)updateScore:(int)increment {
int currentScore = [self getScore];
[self setScore:(currentScore + increment)];
}
Similarly, for label,
#property (nonatomic, retain) CCLabelTTF scoreLabel; // in header
and
#synthesize scoreLabel; // in .m file
Again, in your init method, initialize the label with position, layer and initial text etc. Then you can update that text in the updateScore method.
-(void)updateScore:(int)increment {
int currentScore = [self getScore];
[self setScore:(currentScore + increment)];
[scoreLabel setString:[NSString stringWithFormat:#"Score: %i", [self getScore]]];
}
Make sure you read through the tutorial before going ahead in order to avoid confusion regarding common tasks.

Related

NSMutableDictionary - entries appear to be over-written by each addition

I'm fairly new to Objective-C; but have been coding for years and this one really stumps me.
I'm trying to build an iPhone app and wanted to create a "settings" screen which will use a Table format. (Xcode 5.1.1).
I want to future proof the main Settings screen and make it easy for the application coding by hiding the "hard work" in subroutines/methods.
I may be getting too clever but I've created a class for each 'setting' that contains screen prompts, default values etc and using an Enum to cross-reference it (so the compiler will highlight typos etc)
The problem I'm encountering is that when I add entries to my NSMutableDictionary and use lldb to print the values; every entry seems to have the same "key" and values. I've tried converting the eNum to an NSNumber and also as an NSString -- no difference in the result - so I'm obviously doing something else daft but just can't see it
The following code is from various .m & .h files, I've omitted boring stuff that you always "have to have" to keep it short
// basic x-ref I want to use in my code
typedef NS_OPTIONS(NSInteger, ConfigurationType) {
unDefined = -1,
Server = 0,
Id = 1,
Phone = 2
};
// definition for a "single" Settings value
#interface SettingDefinition : NSObject
#end
#implementation SettingDefinition
ConfigurationType _cfgType;
NSString *_cfgName;
NSString *_screenTitle;
NSString *_value;
- (NSString *)description
{
NSString *className = NSStringFromClass([self class]);
return [NSString stringWithFormat:#"<%#: x%p Type=%d dbKey=%# '%#' -> %#>", className, self, _cfgType, _cfgName, _screenTitle, _value];
}
- (id)initType:(ConfigurationType)cfgOption
withDbKey: (NSString*)dbKey
asOptionTitle:(NSString*)cfgTitle
withValue:(NSString*)itmValue
{
self = [super init];
if (self) {
_screenTitle = cfgTitle;
_cfgName = dbKey;
_cfgType = cfgOption;
_value = itmValue;
}
return self;
}
#end
#interface Configuration : NSObject
#end
#implementation Configuration {
NSMutableDictionary *Settings; // List of Setting structures
};
- (id)init {
self = [super init];
if (self) {
Settings = [[NSMutableDictionary alloc]init];
[self add:Server withDbKey:#"Server" asOptionTitle:#"Server"];
[self add:Id withDbKey:#"Id" asOptionTitle:#"Your ID"];
[self add:Phone withDbKey:#"Phone" asOptionTitle:#"Phone No."];
}
return self;
}
- (void) add:(ConfigurationType)cfgOption
withDbKey:(NSString*)dbKey
asOptionTitle:(NSString*)cfgTitle
{
NSString * itmValue = [self configurationValue: cfgOption cfgName:dbKey];
SettingDefinition *x = [[SettingDefinition alloc]
initType: cfgOption
withDbKey: dbKey
asOptionTitle: cfgTitle
withValue: itmValue];
[Settings setObject:x forKey:[self asKey:cfgOption]];
}
- (NSString *) asKey:(ConfigurationType) settingType {
NSString *rc = [NSString stringWithFormat:#"%d", settingType];
return rc;
}
- (NSString *) configurationValue:(ConfigurationType) settingType {
// returns a suitable value from my system setup
// which is initially a null value until the user sets everything up
}
the debug window shows the following when I break after the final call to [self add: ...]
(lldb) po Settings
{
0 = "<SettingDefinition: x0x8e7c280 Type=2 dbKey=Phone 'Phone No.' -> (null)>";
1 = "<SettingDefinition: x0x8c703a0 Type=2 dbKey=Phone 'Phone No.' -> (null)>";
2 = "<SettingDefinition: x0x8e7c310 Type=2 dbKey=Phone 'Phone No.' -> (null)>";
}
The (null) is obviously due to no data in 'value' yet; but why do they all show as 'Phone'; if I break after the second call to [self add:..] they all show as 'Id'
UPDATE:
DOH! obviously they're globals (I've been using another IDE where everything is local until exposed) .. If I enclose them in braces in the implementation as the documentation states then the exhibited problem vanishes. I have properties to access the variables but as the setter does more than just set the memory, I thought I'd need my "own" variables to hold the data.. said it was something daft .. thank you!

Store an array of NSObject Pointers in C array

I'd like to create an NSObject subclass that contains a few member vars:
#interface PointMass : NSObject
{
CGPoint mCurPosition;
CGPoint mLastPosition;
CGPoint mAcceleration;
}
-(id) initWithPosition:(CGPoint*) pos;
#import "PointMass.h"
#implementation PointMass
-(id) initWithPosition:(CGPoint*)pos
{
mCurPosition = *pos;
mLastPosition = *pos;
mAcceleration = ccp(0,0);
return self;
}
#end
And I would like to create a C-style array to hold a bunch of them within a cocos2d class:
// mNumPoint declared in interface, I've set it to 100
PointMass *pointMassList;
pointMassList = malloc(sizeof(PointMass*) * mNumPointMass);
for (int = 0; i < mNumPointMass; i++)
{
CGPoint point = ccp(100,100);
PointMass *p = [[PointMass alloc] initWithPosition: &point];
pointMassList[i] = p;
}
But I get an error
Expected method to write array element not found on object of type 'PointMass *'
Do I need to tell the compiler more about my PointMass Object if I want to store pointers to it in a C array?
I'm basically trying to have a play around with some particle math on iPhone without needing to unpack points from an NSArray constantly if it isn't clear what I'm trying to achieve here.
If I've gone about this in a backwards way I'd love to be corrected - it has been a while since I wrote vanilla C and I'm a little rusty!
it has been a while since I wrote vanilla C
You should still be able to make the distinction between a pointer-to-T and a pointer-to-pointer-to-T (T being PointMass in this case). You want to store an array of PointMass *, and not an array of PointMass (which you couldn't do anyway). So change the declaration of pointMassList to
PointMass **pointMassList;
and it will work. However, if you're using Objective-C anyway, why don't you simply store the instances into an NSArray?

Cannot Get Data from Custom Class

I looked around for an answer to this because it seems simple but I could not seem to find something that suffised. I am trying to test a program I have with fake data. I created a StudentData class that possesses a attributes for a student and also has an array to hold them all.
When another class needs them they call a getStudent method (which is the only public part of this class thus far) and sends an ID number to get the name of that student back.
My problem is that I cannot figure out how to parse through the student array to match the student ID numbers with the one that was passed in. Holding the same problem, I cannot figure out how to pull the name out of a student object either.
Here is my code so far:
// StudentData.m
#import "StudentData.h"
#interface StudentData() {
NSString *ID;
NSString *firstName;
}
#property (nonatomic, strong) NSMutableArray *studentArray;
#end
#implementation StudentData
#synthesize studentArray = _studentArray;
- (NSMutableArray *)studentArray {
if(!_studentArray) _studentArray = [[NSMutableArray alloc] init];
return _studentArray;
}
- (void)awakeFromNib {
NSArray *idA = [NSArray arrayWithObjects:#"111", #"685", nil];
NSArray *fnA = [NSArray arrayWithObjects:#"Mark", #"Sam", nil];
for(int i = 0; i < idA.count; i++) {
StudentData *tempCurrentStudent = [[StudentData alloc] init];
ID = [idA objectAtIndex:i];
firstName = [fnA objectAtIndex:i];
[self.studentArray addObject:tempCurrentStudent];
}
}
- (NSString *)getStudentsFirstName:(NSString *)studentID {
NSString *firstName;
for(int i = 0; i < self.studentArray.count; i++) {
if([studentID isEqualToString:[[self.studentArray objectAtIndex:i] self.firstName]]) { // ERROR
fN = [[self.studentArray objectAtIndex:i] self.firstName]; // ERROR
}
}
return firstName;
}
#end
The Error I keep getting is: Expect ']' and the error is pointing to the "self.firstName" lines.
If I use "[self.studentArray objectAtIndex:i].firstName;" I get the error: Property 'firstName' not found on object of type 'id' I even get this error if I make "firstName" a property (both local or public). This makes sense because the array is technically filled with 'id' types. I also know that unlike java I cannot use generics or anything like that.
Any help would be much appreciated and if it needs clarification please let me know! By the way this is for an ipod/ipad app if that helps at all.
Thanks!
Two things :
You're having a name conflict here. You are having a local variable called firstName and the parameter passed to your problem function has the same name.
firstNameis NOT a property unless you define it as one. In this case it's just a normal local variabel. Means that you can't access it using self.firstName but just by firstName. That's one of the reason the coding guidelines say that you should name local variables starting with an underscore ( as you are already doing in the studentArrayproperty). This way you can distinguish if it is a local variable or a property.
Best,
Flo

Create an global array containing floating numbers

I wanted to create 2 global arrays which can be updated during the run of the programme.In each update i add one element to zeroth position and deleted the last number
I created the arrays as....
In the .h file..........
//////////////
#interface Shared : NSObject{
NSMutableArray *x;
NSMutableArray *y;
}
#property (nonatomic,retain) NSMutableArray *x;
#property (nonatomic,retain) NSMutableArray *y;
+(Shared*)sharedInstance;
#end
In .m file
staticShared* sharedInstance;
#implementation Shared
#synthesize x;
#synthesize y;
+(Shared*)sharedInstance
{
if (!sharedInstance) {
sharedInstance=[[Sharedalloc]init];
}
returnsharedInstance;
}
-(Shared*)init
{
self = [superinit];
if(self)
{
x=[[NSMutableArrayalloc] init];
x=[NSMutableArrayarrayWithObjects:#"0",#"0",#"0",#"0",#"0",#"0",#"0",nil];
y=[[NSMutableArrayalloc] init];
y=[NSMutableArrayarrayWithObjects:#"0",#"0",#"0",#"0",#"0",#"0",nil];
}
returnself;
}
#end
Then i used to call them and re,ove and added elements using the following code....
[[shared sharedInstance].y removeLastObject];
[[shared sharedInstance].y insertObject:new_element atIndex:0];
[[shared sharedInstance].x removeLastObject];
[[shared sharedInstance].x insertObject:new_element atIndex:0];
In the mean time i call these values and calculate an arithmetic value using an expression.
This seems to work well. But it seems to be an inefficient way to handle floating point numbers which i store in it. As these arrays creates objects. Is there any easy method that i can create a global array containing specified amount of floating point numbers and update it during the run of the programm(array size is fixed) by deleting the last object, and call them back to do calculation?
Please help me!
EDIT 1
To sir deanWombourne
.................................
I implement as you instructed! Can you please go through this and help me to correct 2 errors i get.
IN the .h file
#interface Shared : NSObject{
#private
float input[7];
float output[6];
}
+(Shared*)sharedInstance;
-(void)addNewInput:(float)input1;
-(float *)input;
-(void)addNewOutput:(float)output1;
-(float *)output;
#end
in .m file............
#implementation Shared
-(id)init{
if((self =[superinit])){
for(int n=0; n<7 ;++n)
input[n]=0.00f;
for(int n=0; n<6 ;++n)
output[n]=0.00f;
}
returnself;
}
-(void)addNewInput:(float)input1{
input[0]=input[1];
input[1]=input[2];
input[2]=input[3];
input[3]=input[4];
input[4]=input[5];
input[5]=input[6];
input[6]=input1;
}
-(float *)input {
returninput;
}
-(void)addNewOutput:(float)output1{
output[0]=output[1];
output[1]=output[2];
output[2]=output[3];
output[3]=output[4];
output[4]=output[5];
input[5]=output1;
}
-(float *)output {
returnoutput;
}
#end
When calling it
float reading= (accel_reading)/(1.165969038*1e5f);
[[SharedsharedInstance] addNewInput:reading];
Problems i get
1. In the implementation, it says incomplete implementation (it's a warning not an error)
2. How can i used a for loop to fill array values or is this way ok?
Major problem i get,
When i call it as shown above, program stops running telling
Terminating application due to uncaught exception 'NSInvalidArgumentException', reason '+[SharedsharedInstance]: unrecognized selector sent to class 0x5780'
Please help me through this...............
Your code Smells (and I mean that in the nicest possible way!)
Using two parallel arrays and keeping in sync is a bad design pattern (and a performance hit in quite a few ways!). Especially as there is already a struct that handles storing an x and y at the same time - CGPoint).
You're solving the 'only objects go in arrays' problem by converting your float' primitives toNSString` objects, which is horrendously inefficient - take a look instead at the NSValue class, it's designed to put native C primitives into an object without expensive parsing operations :)
You might also want to look into malloc (and free etc) and deal with the whole problem at the C level - this will mean no objects at all and would be blindingly fast (at the cost of more complicated code).
Hope this helps, if you have any questions just add a comment to this answer :)
EDIT
If all you want to do is store 4 x and y values, then this is probably the easiest way to do it :
#interface Shared : NSObject {
#private
CGPoint points[4];
}
+(Shared *)sharedInstance;
- (void)addNewPoint:(CGPoint)point;
- (CGPoint *)points;
#end
#implementation
- (id)init {
if ((self = [super init])) {
// Start with 0,0 for all your points
for (int n = 0; n < 4; ++n)
points[n] = CGPointZero;
}
return self;
}
- (void)addNewPoint:(CGPoint)point {
// Just move all the points along one and add the new one to the end
// (yes, this could be done in a loop but there's not that much point for 4 points!)
points[0] = points[1];
points[1] = points[2];
points[2] = points[3];
points[3] = point;
}
- (CGPoint *)points {
return points;
}
#end
This gives you a method addNewPoint that removes the first point and adds the new point to the end of your array.
You also get the method points that returns the 4 points. Use it something like :
// To add a point
CGPoint newPoint = CGPointMake(100, 100);
[[Shared sharedInstance] addNewPoint:newPoint];
// To do something with the points (in this case, NSLog them)
CGPoint *points = [[Shared sharedInstance] points];
for (int n = 0; n < 4; ++n)
NSLog(#" Point %i : %#", n, NSStringFromCGPoint(points[n]));
EDIT #2
From your comments, you need two arrays, one with input data and one with output data. Try something like this :
#interface Shared : NSObject {
float inputs[4];
float outputs[5];
}
...
This will give you two arrays to read/write to - one called inputs and the other called outputs. Access them in pretty much the same way you did the ones in my first edit :
float *inputs = [[Shared sharedInstance] inputs];
for (int n = 0; n < 4; ++n)
NSLog(#" Input %i : %f", n, inputs[n]);
float *outputs = [[Shared sharedInstance] outputs];
for (int n = 0; n < 5; ++n)
NSLog(#" Output %i : %f", n, output[n]);
Would a linked list be overkill for what you're trying to achieve? It's not quite as simple as a static array of floats, but makes the removal of the last object and insertion of the zeroth object reasonably simple and fast.
If you want an array containing a specific number of Objects, you can use NSArray, which is static, opposed to NSMutableArray.
As for the array being Global, just implement a singleton class that contains the 2 arrays and provides the associated methods.
in Globals.h:
#interface Globals : NSObject
+ (Globals *) sharedGlobals;
#end
in Globals.m:
#implementation Globals
static Globals *sharedGlobals = nil;
+ (Globals *) sharedGlobals{
#synchronized(self){
if (sharedGlobals == nil){
sharedGlobals = [[self alloc] init];
}
}
return sharedGlobals;
}
you then can access the arrays (after you implemented them) with the following line:
[[Globals sharedGlobals] getArrayX];
Here is a sketch to get you going.
Your array size is fixed and only contains floating point numbers, start with a C array:
double x[] = {0, 0, 0, 0, 0, 0, 0};
double y[] = {0, 0, 0, 0, 0, 0};
The number of elements in these arrays can be calculated rather than hard-coded:
int xCount = sizeof(x)/sizeof(double);
int yCount = sizeof(y)/sizeof(double);
Now use these arrays as a circular buffer, declare a cursor and initialise:
int xCursor = 0;
The item at the front of the queue is at the cursor:
valueAtFrontOfQueue = x[xCursor]; // get the current front item
To remove the value at front and add a new one to the rear replace the value at the cursor with the new value and increment the cursor:
x[xCursor] = newValueForBackOfQueue; // replace it with new item for back of queue
xCursor = (xCursor + 1) % xCount; // and advance cursor using mod arithmetic to it cycles around
No wrapping doubles as objects, no dynamic allocation at all.
Wrap the above up as you see fit, maybe as a class, and you're done.

NSTextField continuous update

I can't figure out how to get an NSTextfield to update automatically, without having to press "Return" or click another text field.
My goal is to input a number into a field and have the other fields update simultaneously. I tried clicking "Continuous" in the text field attributes but it doesn't seem to do anything.
Here is my interface file:
#import <Foundation/Foundation.h>
#interface InchController : NSObject {
IBOutlet NSTextField *centimetersTextField;
IBOutlet NSTextField *inchesTextField;
IBOutlet NSTextField *feetTextField;
}
-(IBAction)convert:(id)sender;
#end
Here is my implementation file:
#import "InchController.h"
#implementation InchController
- (IBAction)convert:(id)sender {
if (sender == inchesTextField) {
float inches = [inchesTextField floatValue];
[feetTextField setFloatValue:(inches * 0.0833)];
[centimetersTextField setFloatValue:(inches * 2.54)];
}
else if (sender == feetTextField) {
float feet = [feetTextField floatValue];
[inchesTextField setFloatValue:(feet * 12)];
[centimetersTextField setFloatValue:(feet * 30.48)];
}
else if (sender == centimetersTextField) {
float centimeters = [centimetersTextField floatValue];
[inchesTextField setFloatValue:(centimeters * 0.394)];
[feetTextField setFloatValue:(centimeters * 0.033)];
}
}
#end
So here is the updated implementation file per Josh's solution. Commented out the IBAction since it is no longer needed in the implementation and interface files.
#import "LengthController.h"
#implementation LengthController
//- (IBAction) convert: (id)sender {
//}
-(void) controlTextDidChange:(NSNotification *) note {
NSTextField *changedField = [note object];
if (changedField == inchesTextField) {
float inches = [inchesTextField floatValue];
[feetTextField setFloatValue: (inches * 0.0833)];
[centimetersTextField setFloatValue: (inches * 2.54)];
}
if (changedField == centimetersTextField) {
float centimeters = [centimetersTextField floatValue];
[inchesTextField setFloatValue:(centimeters * 0.394)];
[feetTextField setFloatValue:(centimeters * 0.033)];
}
if (changedField == feetTextField) {
float feet = [feetTextField floatValue];
[inchesTextField setFloatValue:(feet * 12)];
[centimetersTextField setFloatValue:(feet * 30.48)];
}
}
#end
Make your controller the delegate of the text fields; you can set this in Interface Builder by Ctrl-dragging from the text fields to the controller.
In your controller, implement the "NSControl Delegate" method controlTextDidChange:, which will be called (as its name suggests) whenever the field's text changes. In that method, you can validate the text and, if appropriate, update the contents of the other fields.
The argument that is passed in can give you the text field which changed; you can then pass that on to your existing convert: method to reuse the code:
- (void) controlTextDidChange: (NSNotification *)note {
NSTextField * changedField = [note object];
[self convert:changedField];
}
There's nothing special about action methods. The IBAction return type evaluates to void; it's only used by Xcode to expose the method for use in Interface Builder. You can, therefore, call them just like any other method. Here, you get the appropriate field and pass it in as the sender parameter, as if the field had called the action method itself.
Depending on the complexity of the problem, bindings may be a viable solution, too.
You can define properties on a model or model controller object and hook them up to the corresponding text fields. Then, changes in the text field are immediately reflected in the properties, which can then trigger changes to other properties.
Text fields bound to these "derived" properties are then updated automatically.
Remember to "bracket" your changes to the derived properties with willChangeValueForKey: and didChangeValueForKey: so the changes are sent to observers. More here.
Of course, if you have loops in the dependencies, it gets ugly; in that case the controlTextDidChange: method mentioned in the other answers is probably better.