Skip to main content

Objective C create new class which is an image?



Sorry I'm new to Objective C and OOP, and I'm trying to understand how to use classes. I have searched everywhere but can't find a clear answer.





So in my game I want a NEW ball to be created when i tap and drag an image of a ball. Would I create a ball class, and when I tap the ball, an instance of the ball class will be created. How do I set that class to be an image of a ball?


Comments

  1. You didn't provide much detail, but i guess you will create more than one ball in some View controlled by a whateverViewController. One simple approach would be a collection class holding the balls created.

    IN your whateverViewController class :

    ....
    @property (nonatomic,retain) NSMutableSet *balls; // release in dealloc

    On tap - a ball gets created and added to the set.

    Your ball class then implements whatever you need for a ball and holds a UIImage for display.
    ....
    @property (nonatomic,retain) UIImage* ballImage; // release in dealloc

    ReplyDelete
  2. You didn't explain well your game, but I see two scenarios:


    You have multiple balls in the screen and you want a new ball to appear when you tap one of these balls. The ball new ball is similar to the ball that was tapped not only in shape (image) but also similar in behavior, so if you tap this new ball another new ball will appear.
    You have in your UI images of balls. These images are similar to buttons: when you tap one of them, a ball is added to the screen. This new ball has the same shape as the image that was tapped, but it does not have a similar behavior, so you can't tap this new ball to make another new ball appear. But you still can tap the image.




    In the first case, all balls are instances of the class Ball. Each ball has an image that represents it, and has an action that is triggered when the ball is tapped.

    @class Ball

    @property UIImage *image; // This property will be used to draw the ball

    - (void)imageTapped; // This method will be called when the ball it tapped. It will create a new ball.

    // ...

    @end




    In the second case, you still have a Ball class, but it doesn't have the method imageTapped:

    @class Ball

    @property UIImage *image; // This property will be used to draw the ball

    // ...

    @end


    You will also have buttons (or other UI elements) that are displayed with the same image, and when they are tapped, the call the method imageTapped to create the new ball.



    Finally, note that the class Ball may have other properties (size, position, ...) and methods.



    Edit

    You should have an array (NSMutableArray) called balls and initialize it in the method viewDidLoad of the view delegate.

    - (void)viewDidLoad
    {
    // ...
    self.balls = [NSMutableArray array];
    }

    - (void)addBall
    {
    // create ball
    [self.balls add:ball];
    }


    Then, every time you create a ball, you add it to the array.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...