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

2018/12/9

Int share between actors

Nahquin Nahquin

2018/12/9

#
Hay have an issue with int share between actors. I read a lot about this int this site but i cant do it dont know why... Can u pl help me?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Example extends Moduls
{
    int Int=0;
    public void act() 
    {
        // Add your action code here.
    }    
}
----------------------------------------------------
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Example2 extends Moduls
{
    int Int2=0;
    public void act() 
    {
        // Add your action code here.
    }    
    public void something(){
        Int2++;
    }
    public Example2(){
        somethhing();
    }
}
So i want to move the Int2 Into the Example.class and do a Int=Int+Int2 finale score. How can i do this?
danpost danpost

2018/12/9

#
Add fields into your World subclass (hopefully where these actors are created, called MyWorld) to hold these actors:
Example example = new Example();
Example2 example2 = new Example2();
Do not use 'new Example()' or 'new Example2()' later in the class. Then, in Example class methods, you can use:
int x2int = ((MyWorld)getWorld()).example2.Int2;
Nahquin Nahquin

2018/12/9

#
Umm so i solved like:
public class Booster extends Moduls
{   
    public Booster(){
        newSize(2);
    }
    public void act(){
        kocc_Bomber();
    }  
    public void kocc_Bomber(){
        Actor b = getOneIntersectingObject(Bomber.class);
        if (b != null){
            
            setLocation(0,0);
            new Bomber().bummPower(1);
        }
    }
}
public class Bomber extends Moduls
{
int bPow=2;
    public void bummPower(int v){
        bPow = bPow + v;
    }
}
And i dont know why but the bPow dont grow :| //the other version for the int share is not good coz i cant solve it without using new Example();
danpost danpost

2018/12/10

#
Nahquin wrote...
i dont know why but the bPow dont grow :|
You are creating a new Bomber object and changing its bPow value -- not that of the bPow of the Bomber object already in the world.
Nahquin Nahquin

2018/12/10

#
I thinked to this is the problem but i dont know how can i change the already existing Bomber's bPow
Nahquin Nahquin

2018/12/10

#
Or maybe just remove the existing Bomber and add a new one with the upgraded int?
danpost danpost

2018/12/10

#
Nahquin wrote...
Or maybe just remove the existing Bomber and add a new one with the upgraded int?
Why? You already get a reference to the one in the world at line 10.
Nahquin Nahquin

2018/12/10

#
u mean the: b. ?
danpost danpost

2018/12/10

#
Nahquin wrote...
u mean the: b. ?
Yes. It just needs to be typecast as a Bomber object so you can call the bummPower method on it.
Nahquin Nahquin

2018/12/10

#
And how can i do that?
danpost danpost

2018/12/10

#
Nahquin wrote...
And how can i do that?
// line 14
((Bomber)b).bummPower(1);
Nahquin Nahquin

2018/12/10

#
Thanks it works!! Sadly i dont know why or what's teh code is.. I mean : ((Bomber)getOneIntersectingObject(Bomber.class);).bummPower(1); is ok but why the Bomber is in () in the start
danpost danpost

2018/12/10

#
Nahquin wrote...
Thanks it works!! Sadly i dont know why or what's teh code is.. I mean : ((Bomber)getOneIntersectingObject(Bomber.class);).bummPower(1); is ok but why the Bomber is in () in the start
The return type of getOneIntersectingObject is Actor. If you were to call an Actor class method on what was returned, provided null was not returned, there is no issue. However, bummPower is a method in the Bomber class; so, to have the code interpreter look in that class for it, it needs to be informed that actor is indeed of Bomber type.
Nahquin Nahquin

2018/12/10

#
Thank u very much.
You need to login to post a reply.