Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.
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.
-(void)createMeteors{
if (meteorArray==nil){ meteorArray = [[NSMutableArray alloc] init]; }
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:
-(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 }
//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) }
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.
} 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;
//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.
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 }
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.
smasher;158542 said:
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.
-(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 }
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.
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.
smasher;158557 said:
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.
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.
I could go up by fours if flakeCounter is declared as a pointer - int* instead of int.
Otherwise... do you move or remove the rock after you find a collision? If not, then you may have the same collision again on the next frame and your flakeCounter will increase very quickly, maybe too quickly for you to see.
Replies
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.
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.
Not compiled or tested!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeFeel free to ask questions, but I won't create the whole project. Maybe you should start with something simpler and work up to this?
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome-(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
// 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
}
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomeand for collision do i need to put them in array or what
like this??
for (int count=0; count < [objectsArray count]; count++){
UIImageView *anItem = [objectsArray objectAtIndex:count];
if(CGRectIntersectsRect(anItem.frame, myCharacter.frame)){
anItem.hidden = YES;
}
}
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeYour view controller needs a touchesBegan method - like so
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomenow i whenever user touch any one of them they will disappear
numberTaps +=1;
if (numberTaps < 100) {
UIImageView *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
PositionX = arc4random() % 360;
position.x = PositionX;
position.y = -30;
bomb.center = position;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:2.1];
[UIView setAnimationBeginsFromCurrentState:YES];
bomb.frame = CGRectMake(bomb.frame.origin.x, (bomb.frame.origin.y + 560.00), bomb.frame.size.width/2, bomb.frame.size.height/2);//w, h as original
[UIView commitAnimations];
// NOT WORKING
if (CGRectIntersectsRect(bomb.frame,wood.frame)) {
//collosion detected
NSLog(@"BOOM!");
}
in touchMove()
do i need to make an array or what for collisions
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeobjectsArray = [[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 ");
newItem.hidden = YES;
}
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomefor (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
PositionX = arc4random() % 360;
position.x = PositionX;
position.y = -30;
bomb.center = position;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:2.1];
[UIView setAnimationBeginsFromCurrentState:YES];
bomb.frame = CGRectMake(bomb.frame.origin.x, (bomb.frame.origin.y + 560.00), bomb.frame.size.width/2, bomb.frame.size.height/2);//w, h as original
[UIView commitAnimations];
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThanks for that
for (int i = 0; i< [objectsArray1 count]; i++){
UIView* currentBomb = (UIView*)[objectsArray1 objectAtIndex:i];
if(CGRectIntersectsRect(currentBomb.frame, fireBall1.frame)){
NSLog(@"Collision ");
}
}
now my collision is working but its not when they are falling
collision is occurring at bottom (when falling image sits at bottom)
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomeint dx = player.frame.origin.x - wood.frame.origin.x;
int dy = player.frame.origin.y - wood.frame.origin.y;
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomehowever, I can't get the touchesBegan to track the motion of each view as it moves.
here's my current touchesBegan:
it loads everything but touching the screen doesn't do anything. Any help?
SkinTune Custom Music Player with Clickwheel
[IMG]http://a1.phobos.a
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomethanks for the help above though
SkinTune Custom Music Player with Clickwheel
[IMG]http://a1.phobos.a
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeImageshack - screenshot20100106at115
I added the following to my application in the .m file.
And I added the following to my .h file
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeYou 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.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeOtherwise... do you move or remove the rock after you find a collision? If not, then you may have the same collision again on the next frame and your flakeCounter will increase very quickly, maybe too quickly for you to see.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome