Hello i was wondering if any of you guys would know how to make random meteorites fall from the top of my screen to the bottom. Thanks
Here's a partial example. You'll want to set up an NSTimer and an NSMutableArray - the NSTimer will call a method that moves all of the meteors on the screen.
When you create the meteors, you'll create them off the top of the screen - if you created them exactly at the top of the screen, then they'd all appear in one straight line.
Code:
-(void)createMeteors{
if (meteorArray==nil){
meteorArray = [[NSMutableArray alloc] init];
}
UIImage *meteorImage = [UIImage imageNamed:@"meteor.png"];
UIImageView *newView;
for (int i = 0; i< 10; i++){ //creates 10 meteors
newView= [[UIImageView alloc] initWithImage: meteorImage];
//pick a position *above* the top of the screen
int x = arc4random()%320;
int y = -arc4random()%480; //negative y = off the top
newView.center = CGPointMake (x,y);
[self.view addSubview: newView]; //adds meteors as subviews
[meteorArray addObject: newView]; //add to array
[newView release];
}
}
-(void)moveMeteors{ //TODO: NSTimer should call this method
//TODO: loop through the array and move each meteor down 1 pixel.
//TODO: if y>480, move the meteor back to the top
}
Obvious improvements would be to give each meteor a random size, and to create a "Meteor" class that could have other attributes like "Speed" - then each meteor could have its own speed too.
Well i cant really comprehend what you were saying. Could you maybe make a project that shows the uses of the code?
You need to look up some things first to understand this code- NSMutableArray, NSTimer, and UIImageView would be good starters. The code I gave you would go in a view controller of some kind.
Feel free to ask questions, but I won't create the whole project. Maybe you should start with something simpler and work up to this?
for (int i = 0; i< 10; i++){ //creates 10 meteors
newView= [[UIImageView alloc] initWithImage: meteorImage];
//pick a position *above* the top of the screen
int x = arc4random()%320;
int y = -arc4random()%480; //negative y = off the top
newView.center = CGPointMake (x,y);
[self.view addSubview: newView]; //adds meteors as subviews
[meteorArray addObject: newView]; //add to array
[newView release];
}
}
-(void)moveMeteors{ //TODO: NSTimer should call this method
//TODO: loop through the array and move each meteor down 1 pixel.
//TODO: if y>480, move the meteor back to the top // HOw to make each object fall one by one i mean at a time there will be only 1 image on screen and when it goes>480 then next will appear
How to make each object fall one by one - I mean at a time there will be only one image on screen and when it goes>480 then next will appear
In that case you only need one object - when it reaches the bottom of the screen you can simply set its y coordinate back to 0 (the top of the screen.) Something like this:
Code:
-(void)moveMeteors{
//move new center down
CGPoint newCenter = myRock.center;
newCenter.y = newCenter.y +10;
if (newCenter.y > 480){ // if off the bottom
newCenter.y=0; //pop to top
newCenter.x = arc4random()%320; //and pick new x position
}
myRock.center = newCenter;
}
thanks and how to respond them on touch .. like if i am able to press those they will disappear and another will fall from top
Your view controller needs a touchesBegan method - like so
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touch detected");
UITouch *touch = [touches anyObject];
CGPoint touchPosition = [touch locationInView:self];
//Todo:
//now you can check the distance from the touch to your imageView,
//and react if it's less than a certain amount
// or use CGRectContainsPoint(rect, point)
}
__________________
Free Games!
Last edited by smasher; 12-08-2009 at 01:54 AM.
Reason: CGRectContainsPoint
CGPoint position;
//do the stuff above to create the position
//pick a position anywhere on screen
//position.x = (arc4random() % 2) * 320; //stick to left or right side
i did this ( i tried on both end in timer() and touchMOve()
objectsArray = [[NSMutableArray alloc] init];
[objectsArray addObject:newItem];
[self addSubview: newItem];
for (int count3=1; count3 < 70; count3++){
if(CGRectIntersectsRect(newItem.frame, wood.frame)){// touchlocation -->both not working
NSLog(@"Collision ");
You're changing the value of count3, but you're not using it anywhere. You need to loop through the whole array, get the object for each index, and do a collision check with that object.
Code:
for (int i = 0; i< [objectsArray count]; i++){
UIView* currentBomb = (UIView*)[objectsArray objectAtIndex:i];
if(CGRectIntersectsRect(currentBomb.frame, wood.frame)){
NSLog(@"Collision ");
}
}
}
what will be the value of my currentBomb as i am just using bomb
here is the code for 100 images falling form top defined in onTimer()
numberTaps +=1;
if (numberTaps < 100) {
bomb = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"smokeball.png" ]];
//bomb.frame = CGRectMake(0, 0, 32, 32);// tp make small
[self addSubview: bomb];
[bomb release];
CGPoint position;
//do the stuff above to create the position
//pick a position anywhere on screen
//position.x = (arc4random() % 2) * 320; //stick to left or right side
can we calculate the value form center of image rather then origin??
int dx = player.frame.origin.x - wood.frame.origin.x;
int dy = player.frame.origin.y - wood.frame.origin.y;
Your view controller needs a touchesBegan method - like so
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"Touch detected");
UITouch *touch = [touches anyObject];
CGPoint touchPosition = [touch locationInView:self];
//Todo:
//now you can check the distance from the touch to your imageView,
//and react if it's less than a certain amount
// or use CGRectContainsPoint(rect, point)
}
I've gotten all of the above to work fine so that each view is created an moved across the screen.
however, I can't get the touchesBegan to track the motion of each view as it moves.
For some reason the image isn't falling down from the top, it just appears on the screen in random places. It doesn't move or fall down from the top. Please help. The codes I used are posted below. Any kind of help is much appreciated.
I added the following to my application in the .m file.
Code:
-(void)viewDidLoad {
[self createMeteors];
}
-(void)createMeteors{
if (meteorArray==nil){
meteorArray = [[NSMutableArray alloc] init];
}
UIImage *meteorImage = [UIImage imageNamed:@"flake.png"];
UIImageView *newView;
for (int i = 0; i< 10; i++){ //creates 10 meteors
newView= [[UIImageView alloc] initWithImage: meteorImage];
//pick a position *above* the top of the screen
int x = arc4random()%320;
int y = -arc4random()%480; //negative y = off the top
newView.center = CGPointMake (x,y);
[self.view addSubview: newView]; //adds meteors as subviews
[meteorArray addObject: newView]; //add to array
[newView release];
}
}
-(void)moveMeteors{
//move new center down
CGPoint newCenter = myRock.center;
newCenter.y = newCenter.y +10;
if (newCenter.y > 480){ // if off the bottom
newCenter.y=0; //pop to top
newCenter.x = arc4random()%320; //and pick new x position
}
myRock.center = newCenter;
}
Your moveMeteors method needs to loop through all of the meteors in the meteorArray. The post I responded to only wanted one meteor at a time.
Code:
-(void)moveMeteors{
for (UIImageView* myRock in meteorArray ){
//move new center down
CGPoint newCenter = myRock.center;
newCenter.y = newCenter.y +10;
if (newCenter.y > 480){ // if off the bottom
newCenter.y=0; //pop to top
newCenter.x = arc4random()%320; //and pick new x position
}
myRock.center = newCenter;
}
}
Thats what I am trying to do as well. I only want one image falling from the top at a time and when that image rolls off the screen another falls. But for some reason, when the view loads the images just appear on the screen as if they were placed there from Interface Builder in one solid location. No movement nothing. I have setup the NSTimer as well, so I am not sure what is wrong and why it's not moving.
Quote:
Originally Posted by smasher
Your moveMeteors method needs to loop through all of the meteors in the meteorArray. The post I responded to only wanted one meteor at a time.
Code:
-(void)moveMeteors{
for (UIImageView* myRock in meteorArray ){
//move new center down
CGPoint newCenter = myRock.center;
newCenter.y = newCenter.y +10;
if (newCenter.y > 480){ // if off the bottom
newCenter.y=0; //pop to top
newCenter.x = arc4random()%320; //and pick new x position
}
myRock.center = newCenter;
}
}
Thats what I am trying to do as well. I only want one image falling from the top at a time and when that image rolls off the screen another falls. But for some reason, when the view loads the images just appear on the screen as if they were placed there from Interface Builder in one solid location. No movement nothing. I have setup the NSTimer as well, so I am not sure what is wrong and why it's not moving.
If you only want one meteor, you should not create the array of meteors; you should just create one meteor "myRock". I also changed it to "y = 0" - if you're only making one rock it makes sense to put it at the top of the screen, not far off the top like the other code.
Code:
-(void)createMeteors{
UIImage *meteorImage = [UIImage imageNamed:@"flake.png"];
myRock= [[UIImageView alloc] initWithImage: meteorImage];
//pick a position *above* the top of the screen
int x = arc4random()%320;
int y = 0; // place at top of the screen
myRock.center = CGPointMake (x,y);
[self.view addSubview: myRock]; //adds meteors as subviews
[myRockrelease];
}
Sweet! It's working! Thank you SO MUCH. If I setup a new NSTimer is it possible to use it to delay image's from falling rather than it just fall immediately after the other disappears.
Quote:
Originally Posted by smasher
If you only want one meteor, you should not create the array of meteors; you should just create one meteor "myRock". I also changed it to "y = 0" - if you're only making one rock it makes sense to put it at the top of the screen, not far off the top like the other code.
Code:
-(void)createMeteors{
UIImage *meteorImage = [UIImage imageNamed:@"flake.png"];
myRock= [[UIImageView alloc] initWithImage: meteorImage];
//pick a position *above* the top of the screen
int x = arc4random()%320;
int y = 0; // place at top of the screen
myRock.center = CGPointMake (x,y);
[self.view addSubview: myRock]; //adds meteors as subviews
[myRockrelease];
}
Check the "//pop to top" line in moveMeters - if you put the new position off the top of the screen a little (like -50 or -100) there will be a delay before it appears.
You don't need more than one NSTimer in a game; you can have just one that triggers on every frame, and start other actions from that one.
Check the "//pop to top" line in moveMeters - if you put the new position off the top of the screen a little (like -50 or -100) there will be a delay before it appears.
You don't need more than one NSTimer in a game; you can have just one that triggers on every frame, and start other actions from that one.
I am trying to setup a collision between the falling image and another image. I have setup the following way, but for some reason, instead of going up by 1. It's going up by different amounts like 5, 13, 4, 10, etc...for example If one image touches the other it may go 5 then it will go to 18 if touched again. But according to the code it should only go up by one.