Jump to content

  • Log In with Google      Sign In   
  • Create Account


Letting a class see another object's variables


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 JDCAce   Members   -  Reputation: 146

Like
0Likes
Like

Posted 20 October 2012 - 09:47 PM

I have a functional pong game, and I'd like to add powerups (such as increase paddle size). I've created a Powerup class, and I'd like to let it handle all the powerup business. However, I'm not sure how to let my Powerup class see the variables of one of my Paddle class objects. That is, I'd like to use paddleLeft.height += paddleLeft.Height in my Powerup class to double the size of the left paddle (but leave the right paddle alone). However, paddleLeft is an instance of my Paddle class, and it's made in my Game1 class. Is there a way to let my Powerup class know I have a paddleLeft instance, and give my Powerup class access to its variables? I could add parameters to the Powerup constructor, but that doesn't seem efficient. Is there another way?

Sponsor:

#2 PhillipHamlyn   Members   -  Reputation: 445

Like
0Likes
Like

Posted 21 October 2012 - 12:39 PM

The powerup class either has to have a public list of Paddle objects, or needs to have access to a registry of Paddle objects, or the Paddle objects need to hook an event declared by the Powerup class such as "PowerIncreased" and take effective action based on the event call. In all cases the Paddle object lifecycle and the Powerup object lifecycle need to intersect such that at some point they are visible to each other, and you can either share references to each other or hook an event.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS