Lots of things on screen, with minimal code

Discussion in 'iOS Programming' started by beige matchbox, May 23, 2010.

  1. beige matchbox macrumors 6502a

    beige matchbox

    Joined:
    Mar 16, 2005
    Location:
    Oxfordshire, UK
    #1
    OK guys, Been fiddling around with the iPhone SDK for a long time, and recently a few things clicked and I can now get things going, yay.

    But I'm still having trouble with objects etc.


    I've got a UFO in a UIImageView that moves around the screen while animating, seems to be doing what I tell it, the problem is to add another UFO doesn't seem to be all that easy, but I get the impression it should be, I'm guessing the way the routines are written needs to be more generic...

    Code:
    - (void)viewDidLoad {
    	
    	ufoPos = CGPointMake(5.0,5.0);
    	
    	
    	[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(moveUfo)    userInfo:nil repeats:YES];
    	[NSTimer scheduledTimerWithTimeInterval:0.5  target:self selector:@selector(animateUfo) userInfo:nil repeats:YES];
    
    }
    
    - (void)moveUfo{
    	
    	theUfo.center = CGPointMake(theUfo.center.x+ufoPos.x, theUfo.center.y+ufoPos.y);
    	
    	if(theUfo.center.x > 296 || theUfo.center.x < 24){
    		ufoPos.x = -ufoPos.x;
    	}
    	
    	if(theUfo.center.y > 468 || theUfo.center.y < 12){
    		ufoPos.y = -ufoPos.y;
    	}
    	
    }
    
    - (void)animateUfo {
    	NSArray *ufoArray = [[NSArray alloc] initWithObjects:
    						 [UIImage imageNamed:@"ufo1.png"],
    						 [UIImage imageNamed:@"ufo2.png"],
    						 [UIImage imageNamed:@"ufo3.png"],nil];
    	
    	[theUfo setAnimationImages:ufoArray];
    	
    	[theUfo setAnimationRepeatCount:1];
    	[theUfo setAnimationDuration:0.5];
    	
    	[self.view addSubview:theUfo];
    	[theUfo startAnimating];
    	[ufoArray release];
    }
    I've been looking around using google, but I'm not telling it to look for the right things, any tips?

    I couldn't even think of a good, descriptive thread title :rolleyes:
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    I must admit I'm a little confused. Is the code you supplied in the superview that contains the NSImageView or is it part of the NSImageView it self, of which you have subclassed?

    For each UFO you want to display, you are going to have to create it. So assuming UFO is a subclass of NSImageView, here is some pseudo code for my thinking;

    Code:
    NSArray *ufoArray = [[NSArray alloc] initWithObjects:
    						 [UIImage imageNamed:@"ufo1.png"],
    						 [UIImage imageNamed:@"ufo2.png"],
    						 [UIImage imageNamed:@"ufo3.png"],nil];
    
    UFO ufo1 = [[UFO alloc] initWithFrame: myRect];
    [ufo1 setImage: (NSImage *) myUFOImage];
    [ufo1 ssetAnimationImages:ufoArray];
    [ufo1 doOtherSetupTasksSuchAsRandomizingStartingPoint];
    
    UFO ufo2 = [[UFO alloc] initWithFrame: myRect];
    [ufo2 setImage: (NSImage *) myUFOImage];
    [ufo2 setAnimationImages:ufoArray];
    [ufo2 doOtherSetupTasksSuchAsRandomizingStartingPoint];
    
    I would randomize the starting point because otherwise the UFOs will likely overlap each other, at least at the start.

    Now you just add those to the view that will contain them.

    Code:
    [myContainerView addSubview: ufo1];
    [myContainerView addSubview: ufo2];
    I think the above will also mean that you won't need this line that you have;
    Code:
    [self.view addSubview:theUfo];
    Another thing to note. the addSubview: method removes the subview from another view it may be in. So I wonder what will happen when you do that for a subview that is already in the superview you are adding it to. I haven't tried it to find out. Hopefully it is harmless.

    Since I'm confused about your code, I'm not sure where to say to place it. It could be in the UFO subclass.
     
  3. beige matchbox thread starter macrumors 6502a

    beige matchbox

    Joined:
    Mar 16, 2005
    Location:
    Oxfordshire, UK
    #3
    Thanks for the reply, I appreciate it!


    Don't worry I'm still feeling out how things work, the terminology, I'm spending most of the time confusing myself...

    I'll have a play and see what happens with your pseudo code.


    I'm more used to programming micro controllers, where I would have a 2 dimensional array with position, velocity etc. stored for each UFO, then have a function update the position for each UFO each time it's called, adding another UFO would be as simple as adding another set of locations tot he array, and telling the update function how many UFO there are... If you follow.

    While I could do pretty much the exact same thing for the iPhone, I'm sure there is an easier way, such as tell a part of the program to draw 5 UFOs and have it get on with it.

    I'm finding it hard to explain when I don't know the correct terms, sorry for being so vague.


    I think my problem may come from not having a good picture of how classes, sub classes interact and how variables are managed etc. I feel like I'm one small step from it clicking, it's frustrating.

    Back to the books :D


    Oh, I commented out

    Code:
    [self.view addSubview:theUfo];
    And it's made no visible difference to the running program, seemed like a good idea at the time...
     
  4. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    Well you have NSArray & NSMutableArray where you could store many instances of UFOs. You could then process through that array to act on each UFO object.

    Here is how to create and add objects to an NSMutableArray. We use a mutable array because it allows for additions and changes.

    Code:
    UFO * ufo;
    NSMutableArray * ufosArray = [[NSMutableArray alloc] init];
    for (iLoop=0; iLoop<2; iLoop++)   // Generate 2 UFOs.
    {
    ufo = [[UFO alloc] initWithFrame: myRect];
    [ufo setImage: (NSImage *) myUFOImage];
    [ufo ssetAnimationImages:ufoArray];
    [ufo doOtherSetupTasksSuchAsRandomizingStartingPoint];
    [ufosArray addObject: ufo];   // ufo retain count now 2 because array also retains it.
    [myContainerView addSubview: ufo];
    [ufo release];   // Clean up ufo to prevent memory leak.
    ufo = nil;
    }
    
    I'll leave it to you to figure out how to read from the array.

    There is a way to get the UFOs in the container view into an array. The problem with it is that if the container view has other non UFO objects you also get those. So you'd have to filter those out if you want to act on the UFOs. Hope that makes sense. :eek:

    If you haven't seen it, check out the Stanford iPhone programming course available via iTunes U. Also there are some helpful links near the top of this forum. You might want to find an introduction book to Object Oriented design. That would definitely help grasp what you are stepping into.

    You are learning many new things simultaneously. Object Oriented design and programming, a new language, a bunch of new frame works, new design patterns, and eventually 'best practices'. Keep it up and it will click.
     

Share This Page