Create an global array containing floating numbers - objective-c

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.

Related

Objective-C source code debug: Thread Error

I cannot seem to solve this problem, im not sure what it is at this point but the tread error wont go away. I cant seem to find what im doing wrong.
This code may be a handful to read (sorry) but its very simple. I'am basically invoking a function from main and passing an array of function values, from there im passing two fractions at a time to a method so it find the LCM(least common multiple) using the denominators and return the value. But a thread error seems to be occurring at the call to the findLCM method from function at the point of where the method is declared.
#interface Fraction: NSObject
#property int numerator, denominator;
-(Fraction *)findLCM:(Fraction *)fraction withXFractions:(int)Xfraction;
#implementation Fraction
-(Fraction *)findLCM:(Fraction *)fraction withXFractions:(int)Xfraction{
int lcmOfFraction = 0;
int a, b;
a = fraction.denominator;
b = self.denominator;
int max =(a>b) ? a : b; // get max value out of the two denominators.
for (int i = 0; i < Xfraction; i++) {
while(1) /* Always true. */
{
if(max%a==0 && max%b==0)
{ lcmOfFraction = max;
break; /* while loop terminates. */
}
++max;
}
}
Fraction *lcmDenominator = [Fraction new];
[lcmDenominator setTo:0 over:max]; //passing just LCM (denominator of 2 fractions)
return lcmDenominator;
}
Fraction *addFraction(Fraction **arrayOfFractions, int arraySize) {
Fraction *LCM = [[Fraction alloc] init];
int lcmOfFractions = 0;
[LCM setTo:0 over:1];
for (int i = 0; i <= arraySize; i++) {
LCM = [LCM findLCM:arrayOfFractions[i] withXFractions:4];
//^gets the LCM (the common denominator)
}
return LCM;
}
int main () {
#autoreleasepool {
[frac1 setTo:2 over:12]; [frac2 setTo:2 over:4];
[frac3 setTo:6 over:8]; [frac4 setTo:8 over:3];
Fraction __autoreleasing *arrayOfFractions[4] = {frac1, frac2, frac3, frac4 };
Fraction *LCMFraction = addFraction(arrayOfFractions, 4);
//common LCM return
}
}
You create no threads in this code. When asking a question like this you should clearly explain what the error is, and what you have tried to find its source. People may then be able to help you.
Some hints/suggestions:
use breakpoints to stop your code during execution to allow you to examine the values of your variables
pay close attention to indexing operations, you are using a C array and those do not do bounds checking
ask whether using a C array here, rather than an NSArray is your best choice. Note there is nothing wrong with using C arrays when appropriate.
__autoreleasing probably doesn't mean what you think it does, it is rare to see this used explicitly in user code.
If you have a specific question when you've got further ask a new question on SO, somebody will probably be able to help you out.
HTH

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?

Cocos2D scoring game [duplicate]

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.

multiple identical pointers, can't figure out why

I'm new to Objective-C, I feel like I'm probably just making a dumb mistake, but I have tried googling this with no luck (maybe I'm just not searching the right things)
Essentially, I tried to write my own object class, and then make several instances of it, but the data seems locked together; Changing the data for any one reference changes it for all of them.
here's where I make the objects and use them.
#implementation Drawing
//leaving many functions out as they are not part of the problem
Ball * balls[10];
int numPoints;
//this gets called first
- (id) initWithCoder: (NSCoder *)aDecoder
{
//leaving out loading of images.....
numPoints=10;
for(int i=0; i<10; i++){
balls[i]=[[Ball alloc] init];
printf("bmem: %p\n",%balls[i]);
float tpx=arc4random()%1024;
float tpy=arc4random()%768;
printf("randX: %f\n",tpx);
printf("randY: %f\n",tpy);
CGPoint tempPt = CGPointMake(tpx,tpy);
printf("mem: %p\n",%tempPt);
[balls[i] setLocation:tempPt];
}
//code to start a timer on the drawRect function....
}
//called regularly, every 30 seconds
- (void) drawRect:(CGRect)rect
{
CGContextRef c=UIGraphicsGetCurrentContext();
CGContextClearRect(c, rect);
for(int i=0; i<numPoints; i++)
{
int ptX=[balls[i] getX];
int ptY=[balls[i] getY];
printf("index: %d\n",i);
printf("x: %d\n",ptX);
printf("y: %d\n",ptY);
CGContextDrawImage(c, CGRectMake((int)ptX-WIDTH/2, (int)ptY-WIDTH/2, WIDTH, HEIGHT), image);
}
}
This program outputs a series of numbers which I thought would be useful.
- "bmem", or the point in memory where the ball is, increments by regular intervals, as expected.
- "randX" and "randY" are completely random, as they should be.
- "mem", or the point in memory with the CGPoint, doesn't change
and this is the ball object:
#implementation Ball
int x;
int y;
-(void)setLocation:(CGPoint)loc{
x=loc.x;
y=loc.y;
}
-(int)getX{
return x;
}
-(int)getY{
return y;
}
#end
At first I just though I had static attributes in the Ball class, but on googling I found out that objective-c does not have static attributes. I've blindly tried about a dozen different things with no success. I really just need this to work.
You are using the global variables:
Ball * balls[10];
int numPoints;
When you probably want instance variables:
#interface Balls : NSObject
{
Ball * balls[10];
int numPoints;
}
...
#end

Int value becoming larger when sent to method

SOLUTION:
Simply separating the method call
Tile *tile = [[Tile alloc] initWithX:x Y:y];
into two separate lines such as:
Tile *tile = [Tile alloc];
[tile initWithX:x Y:y];
caused the method to stop receiving the ints as memory pointers. I'm baffled as to why this occurred but it fixed the problems. Any explanations as to why this happened would be appreciated. Thanks to all for the help!
ORIGINAL POST:
This is a bit of a weird question as I've run into this problem while doing a tutorial at: http://www.iphonegametutorials.com/2010/09/23/cocos2d-game-tutorial-building-a-slide-image-game/.
The program I have typed out works except for the switching of tiles, and I have looked in the XCode debugger and it seems that my x and y values for the Tile class are being set way to absurdly high values. The weird thing is, when the values are passed to the method (initWithX:Y:) from the original method (initWithSize:imgSize:) they are what they should be, but when they arrive at the method they are huge (for example: in one instance an x passed as '1' becomes '1065353216' when it arrives at the method.)
I have a feeling that it may be pointing to the memory location of the passed int, but I don't see any reason why it should be in my code. See below.
-(id)initWithSize:(CGSize)aSize imgSize:(int)aImgValue {
self = [super init];
imgValue = aImgValue;
size = aSize;
OutBorderTile = [[Tile alloc] initWithX:-1 Y:-1];
content = [NSMutableArray arrayWithCapacity:size.height];
readyToRemoveTiles = [NSMutableSet setWithCapacity:50];
for(int y = 0; y < size.height; y++) {
NSMutableArray *rowContent = [NSMutableArray arrayWithCapacity:size.width];
for (int x = 0; x < size.width; x++) {
Tile *tile = [[Tile alloc] initWithX:x Y:y];
[rowContent addObject:tile];
[readyToRemoveTiles addObject:tile];
[tile release];
}
[content addObject:rowContent];
[content retain];
}
[readyToRemoveTiles retain];
return self;
}
-(id)initWithX:(int)posX Y:(int)posY {
self = [super init];
x = posX;
y = posY;
return self;
}
Everything should be the same as in the linked tutorial, but I can't see anybody else complaining of the same problem.
I have also noted that if I go into the debugger and manually change the posX and posY values in the initWithX method that the tile switching code works, so my problem definitely lies with the really large int values forming in that method.
Edit for Jim: Hopefully this shows a more complete picture of the layout I have for the Tile object.
#interface Tile : NSObject {
int x, y, value;
CCSprite *sprite;
}
#property(nonatomic, readonly) int x, y;
#property(nonatomic) int value;
#property(nonatomic, retain) CCSprite *sprite;
#end
#implementation Tile
#synthesize x, y, value, sprite;
// Code is logged in below.
-(id)initWithX:(int)posX Y:(int)posY {
self = [super init];
x = posX;
y = posY;
return self;
}
#end
Somewhere, the x and/or y value are being assigned to a pointer. One thing I would check is to make sure you aren't declaring your ints as pointers to ints. I.e. int *x, etc.