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

2014/12/31

Proplem with getOneIntersectingObject(class)

fejfo fejfo

2014/12/31

#
I seem to have a problem with getOneIntersectingObject. I want to object of a different class to attack each other but I can't seem to get it to work. ( for this I need to get int and that doesn't work ) the code that I'm trieing to use is in BlueSoldier to get ints from redSoldier is in BlueSoldier :
int statHealth = 20; //current base maxhealth
    int statAttack = 10; //current base maxattatc
    int statExperiance = 20; //current experienece

    int health; //current health (formula stat * experice)
    int attack; //current attact (formula stat * experice)

    int statHealthAttacker;
    int statAttackAttacker;
    int statExperianceAttacker;

    int healthAttacker;
    int attackAttacker;

    boolean moved = false;
    boolean attacked = false;
    boolean selected = false;
    boolean attackMode = false;

    boolean controledByPlayer = true;

public void attack() {
        statExperianceAttacker = getOneIntersectingObject(SoldierRed.class).getStatExperiance();
        int activeEperiance = (int) ((statExperiance *1.5) - statExperianceAttacker);
        getOneIntersectingObject(SoldierRed.class).health = getOneIntersectingObject(SoldierRed.class).health - (activeEperiance * statAttatck);
    }
in RedSoldier
int statHealth = 20; //current base maxhealth
    int statAttack = 10; //current base maxattatc
    int statExperiance = 20; //current experienece

    int health; //current health (formula stat * experice)
    int attack; //current attact (formula stat * experice)

    int statHealthAttacker;
    int statAttackAttacker;
    int statExperianceAttacker;

    int healthAttacker;
    int attackAttacker;

    boolean moved = false;
    boolean attacked = false;
    boolean selected = false;
    boolean attackMode = false;

    boolean controledByPlayer = true;

public int getStatExperiance() {
        return statExperiance;
    }

    public void setStatExperiance(int newXp) {
        statExperiance = xp;
    }
I have tried it with out getters en setters to ( just .statExperiance) but it didn't seem to work ether. please help me
fejfo fejfo

2014/12/31

#
oeps sommething when wrong whit the copy pasting
danpost danpost

2014/12/31

#
fejfo wrote...
Proplem with getOneIntersectingObject(class)
Believe me ... there is no problem with the method. If no object of the class given (SoldierRed) is intersecting the actor (blue soldier), then 'null' will be retuned from the method and 'null' does not have a 'statExperiance' of any kind. You need to break line 23 of the blue soldier class code above up a bit so you can check for 'null' before executing the 'getStatExperiance' method on it:
SoldierRed red = (SoldierRed) getOneIntersectingObject(SoldierRed.class);
if (red == null) return; // exit method if not intersecting a red soldier
int activeExperiance = (statExperiance*3-red.getStatExperiance*2)/2;
red.health -= activeEperiance*statAttatck;
fejfo fejfo

2015/1/1

#
I didn't mean there was a porblem with getOneIntersectingObject(class) but that I had a problem using getOneIntersectingObject(class) and I made it that attack() never is gonna execute :
if(isTouching(SoldierRed.class) && World2.blueTurn && attacked) {
            attack()
}
(in public void act() ) but know know I have to test in the method:)
You need to login to post a reply.