This site requires JavaScript, please enable it in your browser!
Greenfoot back
ProfessionalNoob
ProfessionalNoob wrote ...

2015/3/23

Clicking two objects of the same class for if condition

ProfessionalNoob ProfessionalNoob

2015/3/23

#
Anyone know how to run something after two objects of the same class are clicked? What I have is this and I don't understand how it's not working.
 if(Greenfoot.mouseClicked(this))
        {   
            initialized = true;
        }
        
        if(initialized = true)
        {
            if(Greenfoot.mouseClicked(Cards.class))
            {
                //Do something
            }
        }
danpost danpost

2015/3/23

#
The 'mouseClicked' method of the Greenfoot class requires a World or Actor object (or null), not a class. To determine if any card was clicked, you need to detect a click ANYWHERE by first using 'Greenfoot.mouseClicked(null)'; then, when a click is detected, get a MouseInfo object using 'Greenfoot.getMouseInfo()' and determine if 'getActor', if not 'null', is 'instanceof Cards':
if (initialized && Greenfoot.mouseClicked(null))
{
    MouseInfo mouse = Greenfoot.getMouseInfo();
    Actor mouseActor = mouse.getActor();
    if (mouseActor != null && mouseActor instanceof Cards)
    {
        // do something with '(Cards)mouseActor'
    }
}
Something to know: your line 6, as written above, will always result in a true condition because you are assigning 'true' to the value of 'initialized' inside the 'if' clause. Either use the conditional equality symbol '==' or, since 'initialized' is already a boolean value, do not use anything at all:
if (initialized == true)
// or
if (initialized)
ProfessionalNoob ProfessionalNoob

2015/3/23

#
Thanks, but it seems what happens is that the card does something after it's clicked rather then clicking two objects. Right now, I made it so that it changes location to test it.
 
    if(Deployment && Dep)
    {
        if(Greenfoot.mouseClicked(this))
        {
            initialized = true; 
        }
        if(initialized && Greenfoot.mouseClicked(null))
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            Actor mouseActor = mouse.getActor(); 
            if(mouseActor!= null && mouseActor instanceof Cards)
            {
                setLocation(50,50); 
            }
        }
        
    }
  
These are the deployment and dep variables being used.
public void Deployment()
{
    if (!MouseHover && Greenfoot.mouseMoved(this) && !Deployment)
        {
            CardImages[number].scale(NormalWidth, NormalHeight); 
            MouseHover = true;
        }
    if (MouseHover && Greenfoot.mouseMoved(null) && ! Greenfoot.mouseMoved(this))
        {
            CardImages[number].scale(NormalWidth / 2, NormalHeight / 2); 
            MouseHover = false;
        }
    if(MouseHover && Greenfoot.mouseMoved(null) && Greenfoot.mouseMoved(this) && getY() < 400)
    {
        MouseHover = false;
        CardImages[number].scale(NormalWidth / 2, NormalHeight / 2); 
        Deployment = true; 
    }

}
danpost danpost

2015/3/24

#
I doubt you want 'Deployment' to do anything to a card that is already deployed. Replace line 3 with the following two lines:
if (Deployment) return;
if (! MouseHover && Greenfoot.mouseMoved(this))
A major concern with the mouse clicks is that one card is not aware that another might have been initialized and will initialize itself when clicked on (even when it is the target of an attack from another card). The only way to get around that is to hold the initialized card in a static field in the Cards class:
private static Cards attacker;
Then if the field is null when a card is clicked, set the field to that card. If the field has a value, then that card is being attacked by the Cards object held by the field (provided it is not the same card). Make sure to reset it to null after an attack is completed.
if (Deployment && Greenfoot.mouseClicked(this))
{
    if (attacker == null) attacker = this;
    else if (attacker != this)
    {
        // take damage
        // check health
        // deal damage if still alive
        // check health of attacker
        // finalize attack (whatever else needs done)
        attacker = null;
    }
    else attacker = null; // deselects if same card
}
ProfessionalNoob ProfessionalNoob

2015/3/24

#
This is what I got:
 if(Deployment && Greenfoot.mouseClicked(this))
    {
        if (attacker == null) attacker = this;
        else if(attacker != this)
        {
         loseHealth(attack);
         if (health <= 0)
         {
             getWorld().removeObject(this); 
         }
            attacker = null;
        }
        else attacker = null;
    }
public void loseHealth(int damage)
{
    health -= damage;
}
What happens is that the object attacks itself rather than the other card. It appears that I have to use the attack variable for the other object. How would I do that? Should I do Cards.attack?
danpost danpost

2015/3/24

#
ProfessionalNoob wrote...
What happens is that the object attacks itself rather than the other card. It appears that I have to use the attack variable for the other object. How would I do that? Should I do Cards.attack?
Did you try 'attacker.attack'?
ProfessionalNoob ProfessionalNoob

2015/3/24

#
Yes, that works. Thank you so much.
You need to login to post a reply.