referencing an object

  • Thread starter Thread starter Arda Coskun
  • Start date Start date
A

Arda Coskun

Hi,

I have a problem with references in C#.

I have three classes; Game class, Player class and Bonus class.

In Game class,

I have created a Player object and named it myPlayer and set myPlayer's
color to White.
I have created a Bonus object and named it myBonus and send myPlayer to
myBonus as a parameter.

In Bonus class,

I have created a Timer object and named it myTimer.


The thing I want to do is change myPlayer's color from myTimer's Tick event.

I do not want to declare myPlayer's color as static. I want to solve this
with references but how?

Any help would be appriciated.
 
Hi,

Could you give more details what you want to do
with that player's color?

As i understand, You want change a color of player
on timer's tick... but:
- do you want to do a "coloured flash" efect on player?
- do you want to restore original color after this "flashing"?

Cheers!

Marcin
 
Hello

Make the myTimer event handler an instance member of the Player class.

myTimer.Tick += new EventHandler(myPlayer.ChangeColor);

class Player
{
// other Player class members
public void ChangeColor(object sender, EventArgs args) {
this.Color = Colors.White;
}
}

Best regards,
Sherif
 
Hi,

Then i can recomand to do it on:

e.g. you can do it in a body of property "Player":

public class Bonus {
// ...
private Player myPlayer;

public Player Player {
get {
return myPlayer;
}
set {
if( myPlayer!=value ) {
if( myPlayer!=null ) {
// stop timer
}
myPlayer=value;
if( myPlayer!=null ) {
// start timer
}
}
}
}

private timer_TickHandler(object sender, EventArgs e) {
myPlayer.Color = anyColor; // or generated by random
}

// ...
}
 
Thank you Sherif for kind reply,

I wanted to mean, here my Bonus class.

public class Bonus
{
Timer bonusTimer;
const int BONUS_TIME_MAX = 5;
Player tempPlayer

public Bonus(Player myPlayer)
{
bonusTimer = new Timer();
bonusTimer.Enabled = true;
bonusTimer.Interval = 1000;
seconds = 0;
isBonusFinished = false;
this.bonusTimer.Tick += new
System.EventHandler(this.bonusTimer_Tick);
}

public void setProperties(ref Player myPlayer)
{
tempPlayer = myPlayer
tempPlayer.ChangeColor(Color.White);
}

public void setPropertiesBack(Player tempPlayer)
{
tempPlayer.ChangeColor(Color.Black);
bonusTimer.Enabled = false;
}

private void bonusTimer_Tick(object sender, System.EventArgs e)
{
if(++seconds == BONUS_TIME_MAX)
{
setPropertiesBack(tempPlayer);
}
}
}

In Game class;

I'm creating a bonus,

Player playerOne = new Player(Color.Red);
Bonus myBonus = new Bonus(playerOne);

When user gets bonus, I want just invoke setProperties(playerOne method and
myBonus object will change color of playerOne to white first and after 5
seconds to black.

How can I do this?

Thanks.
 
Hi,

I wanted to mean, here my Bonus class.

public class Bonus
{
Timer bonusTimer;
const int BONUS_TIME_MAX = 5;
Player tempPlayer

// was missing
int seconds;
public Bonus(Player myPlayer)
{
bonusTimer = new Timer();
bonusTimer.Enabled = true;
bonusTimer.Interval = 1000;
seconds = 0;
isBonusFinished = false;
this.bonusTimer.Tick += new
System.EventHandler(this.bonusTimer_Tick);
}

public void setProperties(ref Player myPlayer)
{
tempPlayer = myPlayer
tempPlayer.ChangeColor(Color.White);
}

If Player is a class... then "ref" is not welcome here.
A Objects (class instances) are passed by reference,
so you don't need to pass the references to object
variables.
public void setPropertiesBack(Player tempPlayer)
{
tempPlayer.ChangeColor(Color.Black);
bonusTimer.Enabled = false;
}

private void bonusTimer_Tick(object sender, System.EventArgs e)
{
if(++seconds == BONUS_TIME_MAX)
{
setPropertiesBack(tempPlayer);

// In this case you can easily turn the timer off
// because after this line player's color will be
// BLACK
bonusTimer.Enabled=false;
}



When user gets bonus, I want just invoke setProperties(playerOne method and
myBonus object will change color of playerOne to white first and after 5
seconds to black.

Do You want that color to change after 5 next seconds or to stay black?

Cheers!

Marcin
 
Hi Arda,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to change the color for a
player object after certain interval. If there is any misunderstanding,
please feel free to let me know.

Base on the code you have provided, I think we can make some changes to the
constructor and the Tick event handler.

public Bonus(Player myPlayer)
{
this.tempPlayer = myPlayer;
bonusTimer = new Timer();
bonusTimer.Enabled = true;
bonusTimer.Interval = 1000;
seconds = 0;
isBonusFinished = false;
this.bonusTimer.Tick += new
System.EventHandler(this.bonusTimer_Tick);
}

private void bonusTimer_Tick(object sender, System.EventArgs e)
{
if(++seconds == BONUS_TIME_MAX)
{
this.tempPlayer.ChangeColor(Color.White);
}
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top