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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.