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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex