Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
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.

Random Rocks

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
Post edited by therealnosserman on
«134

Replies

  • smashersmasher Posts: 3,858Registered Users
    therealnosserman;144822 said:
    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];
    }

    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.

    Not compiled or tested!
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • therealnossermantherealnosserman Posts: 81Registered Users
    Well i cant really comprehend what you were saying. Could you maybe make a project that shows the uses of the code?
  • smashersmasher Posts: 3,858Registered Users
    therealnosserman;144868 said:
    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?
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • rahul7starrahul7star Posts: 126Registered Users
    i am using this
    -(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

    }
  • smashersmasher Posts: 3,858Registered Users
    rahul7star;149789 said:


    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
    }

    myRock.center = newCenter;
    }
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • rahul7starrahul7star Posts: 126Registered Users
    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

    and 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;
    }
    }
  • smashersmasher Posts: 3,858Registered Users
    rahul7star;149811 said:
    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


    - (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)
    }
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • rahul7starrahul7star Posts: 126Registered Users
    see i am using this for making 100 images fall form top
    now 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
  • smashersmasher Posts: 3,858Registered Users
    rahul7star;149819 said:

    in touchMove()
    do i need to make an array or what for collisions
    Yes, you'll need to loop through all of the meteors (or are they bombs?) and check each one to see if it intersects the target.
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • rahul7starrahul7star Posts: 126Registered Users
    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 ");

    newItem.hidden = YES;
    }
  • smashersmasher Posts: 3,858Registered Users
    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.


    for (int i = 0; i< [objectsArray count]; i++){

    UIView* currentBomb = (UIView*)[objectsArray objectAtIndex:i];

    if(CGRectIntersectsRect(currentBomb.frame, wood.frame)){
    NSLog(@\"Collision \");
    }

    }
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • rahul7starrahul7star Posts: 126Registered Users
    Hi
    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

    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];
  • rahul7starrahul7star Posts: 126Registered Users
    Hi
    Thanks 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)
  • rahul7starrahul7star Posts: 126Registered Users
    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;
  • bachonk!bachonk! Posts: 106Registered Users
    smasher;149817 said:
    Your view controller needs a touchesBegan method - like so


    - (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.

    here's my current touchesBegan:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    UITouch *touch = [touches anyObject];

    for(UIView *aView in [self.view subviews])
    {
    CGPoint touchPosition = [touch locationInView:aView];
    if (CGRectContainsPoint(aView.frame, touchPosition) == YES)
    {
    [aView removeFromSuperview];
    }
    }
    }



    it loads everything but touching the screen doesn't do anything. Any help?
  • bachonk!bachonk! Posts: 106Registered Users
    Used this code and it works great:


    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    UITouch *touch = [touches anyObject];
    CGPoint touchPosition = [touch locationInView:self.view];
    for(UIView *aView in [self.view subviews])
    {
    //CGPoint touchPosition = [touch locationInView:aView];
    if (CGRectContainsPoint(aView.frame, touchPosition) == YES)
    {
    [aView removeFromSuperview];
    }
    }
    }


    thanks for the help above though
  • kdp8791kdp8791 Posts: 57Registered Users
    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.

    Imageshack - screenshot20100106at115

    I added the following to my application in the .m file.


    -(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;
    }


    And I added the following to my .h file


    UIImageView *myRock
    NSMutableArray *meteorArray;

    @property(nonatomic, retain) UIImageView *myRock;
    @property(nonatomic, retain) NSMutableArray *meteorArray;
  • kdp8791kdp8791 Posts: 57Registered Users
    I have also added an NSTimer as well.

    [NSTimer scheduledTimerWithTimeInterval:(4) target:self selector:@selector(moveMeteors) userInfo:nil repeats:YES];
  • smashersmasher Posts: 3,858Registered Users
    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
    }

    myRock.center = newCenter;
    }
    }

    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • kdp8791kdp8791 Posts: 57Registered Users
    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
    }

    myRock.center = newCenter;
    }
    }

  • smashersmasher Posts: 3,858Registered Users
    kdp8791;158550 said:
    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.


    -(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];
    }
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • kdp8791kdp8791 Posts: 57Registered Users
    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.


    -(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];
    }
  • smashersmasher Posts: 3,858Registered Users
    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.
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
  • kdp8791kdp8791 Posts: 57Registered Users
    smasher;158586 said:
    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.


    -(void)checkcollision {
    if(CGRectIntersectsRect(floatingball.frame, myRock.frame)) {

    flakeCounter = flakeCounter + 1;
    flakeInt.text = [NSString stringWithFormat:@\"%d\", flakeCounter];

    }
    }
  • smashersmasher Posts: 3,858Registered Users
    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.
    [IMG]http://a5.mzstatic.com/us/r1000/042/Purple/2d/83/7d/mzm.cimuqibw.75x75-65.jpg[/IMG] <a href="http://itunes.apple.com/us/app/tiny-nightclub/id418478743?mt=8" ta
Sign In or Register to comment.