Abdul Ahmad
Abdul Ahmad

Reputation: 10021

iOS dynamic object creation

I've worked with Xcode and iOS on a few personal projects and have always used non-object-oriented designs for everything... just because I've been doing mostly learning/experimenting with things.

Now I've been trying to implement some object oriented design into one game I've made previously. The idea is, I have a space ship that can shoot bullets. In the past I basically added the UIImageView to the storyboard and then connected it to the .h file and from there did things on it like move it around or whatever (using CGPointMake).

The idea now is to make a method (and a whole other class soon) that will create a UIImageView programmatically, allocate it, add it to the superview etc... I've got this working so far, easy stuff, but the next part is a bit harder. Where do I store this local variable "bullet"? I've been saving it to an NSMutableArray and doing the following: (actually here are the methods that I have)

-(void)movement {
    for (int i = 0; i < [array1 count]; i ++) {
        UIImageView *a = [array1 objectAtIndex:i];
        a.center = CGPointMake(a.center.x + 2, a.center.y);
        if (a.center.x > 500) {
            [array1 removeObjectAtIndex:i];
            [a removeFromSuperview];
        }
    }
}

-(void)getBullet {
    UIImageView *bullet = [[UIImageView alloc] initWithFrame:CGRectMake(ship.center.x + 20, ship.center.y - 2, 15, 3)];
    bullet.image = [UIImage imageNamed:@"bullet2.png"];
    bullet.hidden = NO;
    [self.view addSubview:bullet];
    [array1 addObject:bullet];
}

(by the way, array1 is declared in the .h file) and theres a timer that controls the movement method

timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(movement) userInfo:nil repeats:YES];

first question is: what is the correct way of doing this? Storing a bullet for example until it is removed from the superview, should I store it another way?

and another question is, when I remove a UIImageView from the superview, does that remove it from memory so its not using up system resources?

Thank you for the help! (will update if I Think of other questions

UPDATE I would like to point out that currently this is not functioning correctly. When I click on a "shoot" button that I have created (which basically calls the getBullet method) the bullet appears but does not move.. not sure why. This is part of why I'm asking this question as well.

Upvotes: 0

Views: 301

Answers (1)

Brian Tracy
Brian Tracy

Reputation: 6831

I offer you a totally different solution. Instead of using the built in UIKit to perform graphics calculation and move things around the screen, try using an external framework other than UIKit. The UIKit framework is really good at its intended job, which is more oriented around a touch based user interface than game development. The following is pulled from the UIKit documentation from Apple.

The UIKit framework provides the classes needed to construct and manage an application’s user interface for iOS. It provides an application object, event handling, drawing model, windows, views, and controls specifically designed for a touch screen interface.
As you can see, UIKit was made for the purposes of slick user interfaces, not animating sprites (spaceships, bullets) around the screen.

The best alternative is to look down another path. The first thing to go to would be SpriteKit. Made by Apple and already included in Xcode, SpriteKit is the perfect tool for your job. Made specifically for game development, it has the ability to seamlessly animate many sprites on screen at a time (bullets in your case) and has a built in physics engine. It is very easy to learn, and has the potential to turn you idea into a great game.

The link to the SpriteKit documentation can be found here.

Upvotes: 1

Related Questions