Please help! Creating new objects (from array?) after an object completes task - iphone

I am writing an iPhone application and here is the overall synopsis:
An object is on the screen and moves based on accelerometer input - I have that working - it moves to the edge of the screen and doesn't go off = perfect. Now, what I want to happen is as soon as that object hits any of the four screen edges, it should stop and stay put on the screen, and a new object should 'appear' and start moving due to the accelerometer input, so now two objects would be on the screen, but only 1 moving. Eventually there could be 20 objects built up around the edge, but only 1 object will be moving at a time.
So I have now gotten the help I needed to check for edge hits etc, but I am now trying to switch the way I was getting boxes to show up on the screen. I originally was putting images on the screen through the view controller, but now what I want to do is start with one box in the center, when it hits an edge, it should stop and stay, and a new image will appear in the center and start moving due to accel input as described above. So do I just use an array to pull the images from? Do I not even put .png's on the view controller and should I just code it? Here is some of what I have trying to do this through an array:
//In my .h
UIImageView *blocks;
NSString *blockTypes[3];
//In my .m
blockTypes[0] = #"greenBox1.png";
blockTypes[1] = #"greenBox2.png";
blockTypes[2] = #"greenBox3.png";
Thanks in advance for any help! The help so far has been great!

You should't test if newX and newY are equal to 30 and 50. You should test if they are less than 30 and 50 respectively.
Edit:
I would do it like this:
loat newX = [mutableBoxArray lastObject].center.x + (accel.x * 12);
float newY = [mutableBoxArray lastObject].center.y + (accel.y * -12);
if(newX > 30 && newY > 50 && newX < 290 && newY < 430) {
[[mutableBoxArray lastObject] setCenter: CGPointMake(newX, newY)];
} else {
MyBox *myBox = [[MyBox alloc] init];
[mutableBoxArray addObject: myBox];
[myBox release];
}
Edit 2:
Add the following in your interface file
NSMutableArray *mutableBoxArray;
NSArray *imageNamesArray;
Then in your implementation file in the loadView add
mutableBoxArray = [[NSMutableArray alloc] init];
imageNamesArray = [[NSArray alloc] initWithObjects: #"orangeBox1.png",
#"blueBox1.png", #"greenBox1.png", #"pinkBox1.png", nil];
Then change the above method to
static NSInteger imageInt = 0;
loat newX = [mutableBoxArray lastObject].center.x + (accel.x * 12);
float newY = [mutableBoxArray lastObject].center.y + (accel.y * -12);
if(newX > 30 && newY > 50 && newX < 290 && newY < 430) {
[[mutableBoxArray lastObject] setCenter: CGPointMake(newX, newY)];
} else {
if (imageInt < [imageNamesArray count]) {
UIImage *image = [UIImage imageNamed: [imageNamesArray objectAtIndex: imageInt++]];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
[imageView setCenter: CGPointMake(100.0f, 100.0f)];
[mutableBoxArray addObject: imageView];
[imageView release];
}
}

Related

How to match image shape in UIGestureRecognizer in iOS?

I want to make shape match app like this.
Please check the second image of this link.
please check this link also,
I can drag and drop image but I am unable to match the exact location and unable to fill that image with another one.
Any idea or suggestions would be highly welcome.
//UIImageView *imageView1 (blank image with whom the image has to be matched),
//UIImageView *imageView2 (the coloured image that has to be matched)
Take a NSMutableArray positionArray, now whenever you add any new blank imageview (imageview1 ) to the screen add its additional information to the MutableArray like
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,100,50)];
[imageview setTag:TagValue];
[imageview setImage:[UIImage imageNamed:#"blankMonkeyShapedImage.png"]];
//Incrementing the TagValue by one each time any blank imageview is added
TagValue++;
NSMutableDictionary *locationDic=[[NSMutableDictionary alloc] init];
[locationDic setValue:[NSNumber numberWithInt:imageview.tag] forKey:#"TagNumber"];
[locationDic setValue:[NSString stringWithFormat:#"Monkey~example"] forKey:#"ImageName"];
[locationDic setValue:[NSNumber numberWithFloat:imageview.frame.origin.x] forKey:#"PosX"];
[locationDic setValue:[NSNumber numberWithFloat:imageview.frame.origin.x] forKey:#"PosY"];
[locationDic setValue:[NSNumber numberWithFloat:imageview.frame.size.width] forKey:#"SizeWidth"];
[locationDic setValue:[NSNumber numberWithFloat:imageview.frame.size.height] forKey:#"SizeHeight"];
[self.view addSubview:imageview];
[positionArray addObject:locationDic];
Now whenever you add any new blank imageview you should repeat the same for it . Coming back to the UIGestureRecognizer selector method
-(void)panGestureRecognizer:(UIPanGestureRecognizer *)gesture
{
CGPoint translation = [gesture translationInView:self.view];
for (int i=0 ; i<[positionArray count]; i++)
{
NSMutableDictionary *lctionDic=[positionArray objectAtIndexPath:i];
float posX=[[lctionDic valueFor:#"PosX"] floatValue];
float posY=[[lctionDic valueFor:#"PosY"] floatValue];
float SizeWidth = [[lctionDic valueFor:#"SizeWidth"] floatValue];
float SizeHeight = [[lctionDic valueFor:#"SizeHeight"] floatValue];
if (translation.x >= posX && translation.x <= posX+SizeWidth &&
translation.y >= posY && translation.y <= posX+SizeHeight )
{
//Condition where the dragged objects position matches any previousluy placed balnk imageview.
if(gesture.view.tag==[[lctionDic valueFor:#"TagNumber"] intValue])
{
NSLog(#"Matched Image's Name is : %#",[lctionDic valueForKey:#"ImageName"]);
break;
}
else
continue;
}
}
}
Take special care at the time of allocating TagValues to the blank imageview and the toBeDragged imageview (both should be saem for same type of image).
That is bit tricky.
You are coder you know which image match what shape, May be you take two imageviews one with shape and other with image..
PS: I am just giving an idea.
So may be you can keep track with tags. Add gesture recogniser to drag images, so you will come to know which shape is being dragged. Now while moving just compare center of shape with center of your image current position. Make sure that you compare range and not exact center.
Hope this much info helps you :)

can't use a UIKit code in Cocos2D

well I have created a game with UIKit and I have somme performance problems so I decided to use Cocos2D.I would like to use this code one cocos2D but I can't manage to do it :
- (CGPoint)randomPointSquare {
CGRect frame = [[self view] frame];
//CGFloat rand_x = ((float)arc4random() / (float)UINT_MAX) * (float)_window.frame.size.width;
NSInteger side = arc4random() / (UINT_MAX/4);
CGFloat offset = 0;
switch(side) {
case 0: /* top */
offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.width;
return CGPointMake(offset, -10);
case 1: /* bottom */
offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.width;
return CGPointMake(offset, frame.size.height-150);
case 2: /* left */
offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.height;
return CGPointMake(-10, offset);
default:
case 3: /* right */
offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.height;
return CGPointMake(frame.size.width+200, offset);
}
}
-(void) createNewImage {
UIImage * image = [UIImage imageNamed:#"abouffer_03.png"];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setCenter:[self randomPointSquare]];
}
Can someone try to translate this code to use it in cocos2D.Thank you .Sorry for my english I'm french :/
You will probably be best off creating a new cocos2d project from the template and then inserting your game logic into the code as needed while translating the view components as needed.
For example, your UIImage would instead be a CCSprite.
But you might be better to encapsulate your movement code inside an object that contains a CCSprite so you can then just reuse that object.
I'd start by creating a new cocos2d project from the template and looking at the hello world example. Then start by adding a CCSprite to that and make it move around a bit. There's lots of tutorials out there - This for example

UIImageView - How to change speed of reverse animation

I have the following code which animates a volume meter:
float meterIndexFloat = volume * 6;
int meterIndex = (int)roundf(meterIndexFloat);
NSMutableArray *litImages = [[NSMutableArray alloc] init];
for(int i=0; i < meterIndex; i++) {
[litImages addObject:[volumeMeterImages objectAtIndex:i]];
}
volumeMeter.animationImages = litImages;
volumeMeter.animationDuration = 0.4;
volumeMeter.animationRepeatCount = 1;
[volumeMeter startAnimating];
It works well, but I would like to slow down the animation when it reverses itself. Right now the duration is 0.4, but I would like to have it slow down to say 0.7 when the images go in the opposite direction. What's the easiest way to modify it to do this?
Thanks!

Problem with uiimageView animation: animation stops

Here is my code:
-(void) createNewBall {
UIImage * image = [UIImage imageNamed:#"bulle_03.png"];
bulleBouge = [[UIImageView alloc] initWithImage:image];
[bulleBouge setCenter:[self randomPointSquare]];
[[self view] addSubview:bulleBouge];
}
-(void)moveTheBall{
bulleBouge.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);
}
createNewBall is called every two seconds. My problem is that every bulleBouge that is created stops moving after two seconds. I don't know why.
How can I solve this please?
It stops moving b/c u are initializing new bulleBouge every two seconds. You are also leakin memory since you never releasy it before assigning new value to it. so what happens is that after u create the imageView you only keep the reference to the lasts instance, hence only the last one is changing position. To fix this store all your new uiImageViews in an array and move them randomly after two seconds.
-(void) createNewBall {
UIImage * image = [UIImage imageNamed:#"bulle_03.png"];
UIImageView *bulleBouge = [[UIImageView alloc] initWithImage:image];
[bulleBouge setCenter:[self randomPointSquare]];
[bulleBougeArray addObject:bulleBouge];
[[self view] addSubview:bulleBouge];
[bulleBouge release];
}
-(void)moveTheBall{
for(int i=0; i< [bulleBougeArray count];i++){
UIImageView *bulleBouge = [bulleBougeArray objectAtIndex:i];
bulleBouge.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);
}
}
Cyprian is correct. In a simpler form, you're creating a new bulleBouge every two seconds, under the same variable. The program already has one, but since you told it to make a new one under the same ivar it forgets the old one, and thus doesn't move it. You need an array so that each ball can be remembered separately, and thus moved separately as seen in the example code that he posted.

What is the best programmatically approach to display images/icons horizontally?

I'm trying to place the icons / images horizontally in a view that is intended to show details. Maximum number of pictures per row is 6 pieces and I want to have a mechanism that manage line breaks or similar automatically. I suspect it is a custom cell that solves this?
I have tried the following code below, but the images above to add another when I reload the view. Furthermore, the cell's height is not adjusted by the view that the code returned.
-(UIImageView *)fillView{
collage = nil;
collage = [[[UIImageView alloc] initWithFrame:CGRectMake(11, 7, 0, 0)] autorelease];
int rowCounter = 0;
int colCounter = 0;
int nrOfPictures = [paymentImages count];
//max 6 images per row
while (nrOfPictures > 0) {
while (colCounter <= 6 && nrOfPictures != 0) {
UIImageView *iv = [[UIImageView alloc] initWithImage:[paymentImages objectAtIndex:nrOfPictures-1]];
CGRect frame = iv.frame;
frame.origin.x = (frame.size.width + 4) * colCounter;
frame.origin.y = (frame.size.height + 4) * rowCounter;
iv.frame = frame;
[collage addSubview:iv];
[iv release];
colCounter++;
nrOfPictures--;
if (colCounter > 6) {
colCounter = 0;
rowCounter++;
}
}
}
return collage;
}
Can someone head me in the right direction?
You might want to check out this project. http://github.com/kirbyt/KTPhotoBrowser