removing integers from an array - iphone

I need to make an array for the co ordinates of my sprites and i wanted to remove the co-ordinate once the sprite has that co-ordinate from the array as i don't want another sprite having the same co-ordinate, i can't seem to get it to work, there are no errors and when debugging it says nsexception and quits. what am i doing wrong and where do i deallocate the arrays? when i release them in dealloc it says i need to declare them.
CGPoint cg1 = CGPointMake(33,33);
NSValue *cg1obj = [NSValue valueWithCGPoint:cg1];
CGPoint cg2 = CGPointMake(33,97);
NSValue *cg2obj = [NSValue valueWithCGPoint:cg2];
NSMutableArray *numberxy = [[NSMutableArray alloc] init]; int pointcount = 0;
[numberxy insertObject:cg1obj atIndex:pointcount++];
[numberxy insertObject:cg2obj atIndex:pointcount++];
NSMutableArray *sprites = [[NSMutableArray alloc] init]; int spritecount = 0;
[sprites insertObject:red1 atIndex:spritecount++];
[sprites insertObject:red2 atIndex:spritecount++];
for (int i=0; i<3;i++) {
int rpoint = arc4random() % 3;
int rsprite = arc4random() % 3;
CGPoint point = [[numberxy objectAtIndex:rpoint] CGPointValue];
CCSprite *sprite1 = [sprites objectAtIndex:rsprite];
sprite1.position = ccp(point.x,point.y);
if (sprite1.position.x == point.x && sprite1.position.y == point.y) {
[numberxy removeObjectAtIndex:rpoint];
[sprites removeObjectAtIndex:rsprite];
}
}

Your code is weird. Why is xValue defined after it is used? You may have a leak if xValue was defined previously in the same manner and now you are redefining xValue. You then check if X is in the new xValue. There is nothing in the new xValue so the if statement will evaluate to false, so nothing will be removed.
NSNumber *X = [NSNumber numberWithInt:randomNumberx];
[xValue containsObject: numberx];
xValue = [[NSMutableArray alloc]init];
if ([xValue containsObject:X]) {
[numberx removeObject:X];
}
Also what is the point of [xValue containsObject: numberx]; outside of the if statement? It doesn't have a purpose.

Related

How To Change a Variable Name Depending on Another Variable?

I have a UIImageView called platform1, platform2, platform3, etc. I have an int called platformNumber coming from one method (method1) into another method (method2).
Method2 (contents not useful to my question):
CGPoint origin = platform1.center;
CGPoint target = CGPointMake(platform1.center.x, platform1.center.y+10);
CABasicAnimation *bounce = [CABasicAnimation animationWithKeyPath:#"position.y"];
bounce.duration = 0.1;
bounce.fromValue = [NSNumber numberWithInt:origin.y];
bounce.toValue = [NSNumber numberWithInt:target.y];
bounce.repeatCount = 1;
bounce.autoreverses = YES;
[platform1.layer addAnimation:bounce forKey:#"position"];
How do I change it so whereever platform1.center.x appears, it changes to platform[platformNumber] depending in the platformNumber value passed through?
Thanks, any help would be much appreciated!
The answers proposing KVC are not scalable at all; please don't do that. You can't declare a variable at runtime, so would will be stuck with however many image views you could attach a property too.
You need to build an array of your image views and iterate them. I repeat, KVC is not intended for this.
NSMutableArray *imageviews = [[NSMutableArray alloc] init];
//...
[imageViews addObject:platform1];
[imageViews addObject:platform2];
[imageViews addObject:platform3];
//...
Then iterate the array:
foreach (UIImageView *imgView in _imageViews) {
CGPoint origin = imgView.center;
CGPoint target = CGPointMake(imgView.center.x, imgView.center.y+10);
CABasicAnimation *bounce = [CABasicAnimation animationWithKeyPath:#"position.y"];
bounce.duration = 0.1;
bounce.fromValue = [NSNumber numberWithInt:origin.y];
bounce.toValue = [NSNumber numberWithInt:target.y];
bounce.repeatCount = 1;
bounce.autoreverses = YES;
[imgView.layer addAnimation:bounce forKey:#"position"];
}
int index = 3;
_imageViews[index]
returns you platform2 in your example (array start at 0, so subtract one)
Alternatively you can also create a function
-(UIImageView*)getPlatformForNumber:(int)number {
switch(number)
{
case 1:
return platform1;
case 2:
return platform2;
...
}
// should not come here
return nil;
}
and call that function where necessary. Sometimes that solution comes in handy - but the array proposed in davbryn's solution is of course more scalable.

Find whether a point is inside a given polygon?

I have downloaded this code. I want to check whether the users current location belongs to any of the region. For that i have called following method but its always returning FALSE. Below is my code:
-(void)checkPoint
{
CLLocationCoordinate2D mapCoordinate ;
mapCoordinate.longitude=34.54664654;
mapCoordinate.latitude=-117.05646;
NSMutableArray *overlays = (NSMutableArray *)[HHLViewController usStatesAndTerritoryOverlays];
MKMapPoint mapPoint = MKMapPointForCoordinate(mapCoordinate);
for (int i=0; i<[overlays count]; i++)
{
MKOverlayView *overlayview = (MKOverlayView *)[self.stateMapView viewForOverlay:[overlays objectAtIndex:i]];
MKPolygonView *polygonView =[[MKPolygonView alloc] initWithOverlay:overlayview.overlay];
CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];
BOOL mapCoordinateIsInPolygon =
CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO);
NSLog(#"polygon is inside %d",mapCoordinateIsInPolygon);
}
}
My problem is i want always getting NO value in mapCoordinateIsInPolygon eventhough the point belongs to that region.
I dont want to display map so any other options are also welcomed.

iPhone:How can I shuffle buttons in view

I have many no. of buttons on my view and I want to shuffle the buttons(change in position) then how can I shuffle the button in my view.
use arc4random()
and change position of your buttons
In your .h file : UIButton *buttonsarray[10];
In your .m file :
// Make array of button using following way.I used this method to creates button and you can use yours.
- (void)viewDidLoad {
[super viewDidLoad];
float y = 5;
int x = 0;
int count = 0;
for (int i = 0 ; i < 10; i++)
{
count ++;
buttonsarray[i] = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonsarray[i].frame = CGRectMake(x, y, 100, 100);
[buttonsarray[i] setTitle:[NSString stringWithFormat:#"%d",i+1] forState:UIControlStateNormal];
x = x + 105;
[self.view addSubview:b[i]];
if(count == 3)
{
count = 0;
x = 0;
y = y+ 105;
}
}
}
// This function will soufflé your buttons
- (IBAction)btnClicked:(id)sender
{
int n = 10;
int swaper;
for (int i = 0 ; i < 10 ; i++)
{
int r = arc4random()%n;
if(r != swaper){
swaper = r;
CGRect r1 = buttonsarray[i].frame;
buttonsarray[i].frame = buttonsarray[swaper].frame;
buttonsarray[swaper].frame = r1;
n--;
}
}
}
Hope,this will help you..
You can do this
Make an Array with a group of CGPoints you will need to store the points as strings.
In the layoutSubViews method set something like this:
-(void)layoutSubViews{
[self.subviews enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
CGPoint newPoint = CGPointFromString([positionsArray objectAtIndex:idx]);
object.frame = CGRectMake(newPoint.x,newPoint.y,object.frame.size.width,object.frame.size.height);
}];
}
Then you will need to shuffle the positions in the positions array, you can see an example method here :How to Shuffle an Array
Now every time you need to Shuffle the positions you can call -(void)setNeedsLayout on your view.
There are more options but that was the first I thought of.
Good Luck

Image replacement in iphone

I have added some sprite by using the following code and then by checking the rect
intersection I want to replace these sprites with another one.Can you help me out by providing code
to replace these random images.Problem is that the image to be replaced is also taken
randomly.........
yp = 40;
//CCSprite *spr;
movableSprites = [[NSMutableArray alloc] init];
for(int m=0; m<4; m++)
{
do
{
i = 'a';
//NSLog(#"valueeeeeeeee %d",i);
next = arc4random()%maxalphabets;
//NSLog(#"valueeeeeeeeenexttttt %d",next);
i+=next;
//NSLog(#"valueeeeeeeee %d",i);
spr = (CCSprite*)[self getChildByTag:i];//checks if found alreadyyyyyy
NSLog(#"Strrrrrrrr is:%#",spr);
}while(spr);
spNameStr = [NSString stringWithFormat:#"%c3.png",i];
NSLog(#"tagggg %d",i);
NSLog(#"spNameStr is:%#",spNameStr);
spr = [CCSprite spriteWithFile:spNameStr ];
spr.tag = i;
//NSLog(#"tagiiiiii %d",i);
spr.position = ccp(60,yp);
[self addChild:spr z:2];
[movableSprites addObject:spr];
yp+=spr.contentSize.height+35;
}
yp = 40;
NSMutableArray *answerimagesCopy = [NSMutableArray arrayWithArray:movableSprites];
NSLog(#"answer image copy elements areeeee %#",answerimagesCopy);
for(k = 0; k < movableSprites.count; ++k)
{
NSLog(#"inside forrrr");
int j=arc4random()%([answerimagesCopy count]);
NSLog(#"valueee of jjjjjjjj %d",j);
CCSprite *ansimage = [answerimagesCopy objectAtIndex:j];
//NSLog(#"................ %#",ansimage);
p=ansimage.tag;
NSLog(#"tagg..... %d",p);
[answerimagesCopy removeObjectAtIndex:j];
//NSLog(#"tagg..... %d",j);
spNameStr = [NSString stringWithFormat:#"%c4.png",p];
NSLog(#"spNameStr is:%#",spNameStr);
//NSLog(#"tagggg %d",j);
spr = [CCSprite spriteWithFile:spNameStr ];
spr.tag = p+100;
spr.position = ccp(260,yp);
[self addChild:spr z:1];
yp+=spr.contentSize.height+35;
}
I have did this using sprite sheet. if you can create a sprite sheet and then assign that sprite sheet to CCSpriteBatchNode's object like
$ CCSpriteBatchNod * m_meteoritesSprites =[CCSpriteBatchNode batchNodeWithFile:#"Meteo.pvr.ccz"];
and then load plist
$[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"Meteo.plist"];
and then use sprite frames to assign images to your sprite like
$ m_meteoritesSprites =[CCSpriteBatchNode batchNodeWithFile:#"Meteo.pvr.ccz"];
i used TexrturePacker to make sprite sheets
hope this helps you.

Cocos2D: Referencing a classes TMXLayer from another class

Using Cocos2D. I've currently subclassed much of the player sprite and behaviors away from a debug map, and now I am having trouble referencing a TMXLayer from the sprite class. I've tried allocating an instance of the DebugZoneLayer class inside this method from the sprite class and releasing it with strange results. This compiles without errors, but fails to test the if (blocksCollidableGID | blocksCollidable2GID){ conditional because debugZoneLayer.blocksCollidable doesn't mean really anything to this method right now.
Method inside sprite class:
-(BOOL) checkTileCollisionForStrafing:(NSString*)omit{
for(int j = 0; j < _collisPushPointsNums; j++){
NSValue *val = [_collisPushPoints objectAtIndex:j];
CGPoint p = [val CGPointValue];
CGPoint tileCoord;
if(omit==#"y"){
tileCoord = [debugZoneLayer tileCoordForPosition:ccp(_heroSprite.position.x+_vel.x+p.x, _heroSprite.position.y+(p.y+.001))];
}else{
tileCoord = [debugZoneLayer tileCoordForPosition:ccp(_heroSprite.position.x+p.x, _heroSprite.position.y+_vel.y+p.y)];
}
int blocksCollidableGID = [debugZoneLayer.blocksCollidable tileGIDAt:tileCoord];
int blocksCollidable2GID = [debugZoneLayer.blocksCollidable2 tileGIDAt:tileCoord];
if (blocksCollidableGID | blocksCollidable2GID){
NSLog(#"j = %i", j);
return YES;
}
}
return NO;
}