moving a imageview from an array ?

Discussion in 'iOS Development' started by JesseEtzler, Jul 5, 2009.

  1. JesseEtzler

    JesseEtzler New Member

    Joined:
    Jun 7, 2009
    Messages:
    19
    Likes Received:
    0
    Device:
    iPod touch
    Right now my code spawns my imageviews every few seconds but for some reason my velocity code for my movement isn't working for some reason, but when I attach the movement code to a single imageView it works just fine..

    what my code should do is send each imageView down the Y axis as it spawns but all it does is spawn in two seperate places and doesn't move.


    here's my code.





    Code:
    
    
    
    
    
    
    astroidArray = [[NSMutableArray alloc] init];
    	
    	UIImage *astroid = [UIImage imageNamed:@"astroidwhite.png"];
    	
    	
    		
    		UIImageView *astroids = [[UIImageView alloc] initWithImage:astroid];
    		
    		//set X and Y of the new imageView
    		
    		astroids.center = CGPointMake(100 , 100 );
    	
    	astroids.center = CGPointMake(astroids.center.x - astroidVelocity.x ,    astroids.center.y - astroidVelocity.y);
    	
    	astroidVelocity = CGPointMake(0,5);
    
    		
    		//add to array
    		[astroidArray addObject:astroids];
    		
    		//add to the view, so it gets displayed.
    		
    		
    		[self.view addSubview: astroids];
    	
    	
    	
    	
    
    
    
    
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Hmmm... tradeoffs. Using imageNamed caches your images, but destroys memory usage. Using initWithContentsOfFile, adding to the array, and releasing will save your memory usage, but is costlier performance wise.

    What I would do is open one image using initWithContentsOfFile when the containing view is loaded. Each new image would be a copy of the original image, released upon adding it to the array. That would reduce your memory usage and maximize your performance.

    That was completely offtopic and irrelevant to your question. To answer your question, you will need to operate in a threaded manner.

    Step one, create a subclass of UIImageView. In this view, do nothing but synthesize a property called weight.

    Thread one, the main thread, has a timer that fires every 1/60 seconds. Each time the timer fires, it iterates through your array.
    for (int i = 0; i < yourArray.count; i++) { //Not using fast enumeration because we want the current index
    asteroid.center.y += asteroid.weight
    if (asteroid.center.y - (asteroid.bounds.size.height / 2) > BOTTOM_OF_SCREEN) { [asteroid removeFromSuperview]; [yourArray removeObjectAtIndex:i];}
    }[/CODE]

    In your second thread, on a timer that fires at whatever rate is appropriate for your game, determine if a new asteroid should be added. Add an imageview into the array, and set its center to offscreen. [yourArray addObject:asteroid]; The first thread will take care of the rest.


    Something as simple as this, you absolutely can use one thread, but as you get more complicated, you'll be glad you've adopted threaded. By the way, I was super distracted as i typed this out. If I typo'd, or simply misrepresented anything, my bad.


    [EDIT: Also, I didn't bother to read your code. If I repeated what you did, or said what you already know, then my bad.]
  3. JesseEtzler

    JesseEtzler New Member

    Joined:
    Jun 7, 2009
    Messages:
    19
    Likes Received:
    0
    Device:
    iPod touch
    Ok I got the array of objects to spawn and move down the y axis using this code... only problem is once a new object spawns it stops moving the previous object in the array :/ any help ?




    for (UIView *astroids in astroidArray) {

    int newX = astroids.center.x + 0;

    int newY = astroids.center.y + 3;

    astroids.center = CGPointMake(newX,newY);




    }
  4. ipodmail09

    ipodmail09 Member

    Joined:
    Mar 13, 2009
    Messages:
    269
    Likes Received:
    0
    Device:
    2G iPod touch
  5. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
  6. ipodmail09

    ipodmail09 Member

    Joined:
    Mar 13, 2009
    Messages:
    269
    Likes Received:
    0
    Device:
    2G iPod touch
    sorry i did the wrong link my bad

    Please Register or Log in to view images

  7. JesseEtzler

    JesseEtzler New Member

    Joined:
    Jun 7, 2009
    Messages:
    19
    Likes Received:
    0
    Device:
    iPod touch
    Could you point me to the correct link please ?
  8. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    Maybe try using a standard array, or a linked list?

    I have no idea why it isn't working...

    You could also try logging events to debug it.

Share This Page