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

2018/5/1

Detect if an actor is intersecting an actor class

Lavenger Lavenger

2018/5/1

#
I'm having trouble in detecting if an actor is touching another actor belonging to another class in my world because pretty much all of the intersect/touching method are protected Actor class methods... context: I am trying to make it so that if hedgehog touches the ReturnToStart actor(s), hedgehog would return to the starting point. Ofcourse this wouldn't be a problem if i have just one map to play on, but because i have many other maps implemented into world, i have 3 different starting points and the starting point have to change according to the starting point that map has set. These are the 3 class codes that i'm using to code this minigame: Minigame world class code:
import greenfoot.*;
public class Minigame4 extends World
{
    public Ground ground = new Ground(), ground2 = new Ground(), ground3 = new Ground(), ground4 = new Ground(), ground5 = new Ground(), ground6 = new Ground(), ground7 = new Ground(), ground8 = new Ground();
    public Hedgehog hedgehog = new Hedgehog();
    public SimpleActor Nope = new SimpleActor();
    int SX = 0, SY = 0;
    public Minigame4()
    {
        super(1200, 675, 1);
        GreenfootImage bg = new GreenfootImage("sky.jpg");
        bg.scale(2000, 800);
        setBackground(bg);
        switch(Greenfoot.getRandomNumber(3))
        {
            case 0:terrain1(); SX = 99; SY = 487; break;
            case 1:terrain2(); SX = 1084; SY = 493; break;
            case 2:terrain3(); break;

        }
        prepare();
    }
    public void act()
    {
        switch(Greenfoot.getRandomNumber(5))
        {
            case 0: Nope.setImage(new GreenfootImage("Nope, not today fam", 20, new Color(230,70,70,150), new Color(0, 0, 0, 0)));
            case 1: Nope.setImage(new GreenfootImage("Sorry, but no", 20, new Color(230,70,70,150), new Color(0, 0, 0, 0)));
            case 2: Nope.setImage(new GreenfootImage("Nah, not happening", 20, new Color(230,70,70,150), new Color(0, 0, 0, 0)));
            case 3: Nope.setImage(new GreenfootImage("Cheating? Restart~", 20, new Color(230,70,70,150), new Color(0, 0, 0, 0)));
            case 4: Nope.setImage(new GreenfootImage("Nice try there", 20, new Color(230,70,70,150), new Color(0, 0, 0, 0)));
        }
        if (hedgehog.getOneInterectingObject(SimpleActor.class) == null)
        {
            Greenfoot.playSound("Nope.wav");
            hedgehog.setLocation(SX,SY);
            addObject(Nope, 600, 30);
        }
    }

    private void terrain1()
    {
        Ground ground9 = new Ground(), ground10 = new Ground();
        ShortGround shortground = new ShortGround(), shortground2 = new ShortGround(), shortground3 = new ShortGround(), shortground4 = new ShortGround(), shortground5 = new ShortGround(), shortground6 = new ShortGround(), shortground7 = new ShortGround(), shortground8 = new ShortGround(), shortground9 = new ShortGround(), shortground10 = new ShortGround(), shortground11 = new ShortGround(), shortground12 = new ShortGround();
        ReturnToStart returntostart = new ReturnToStart(), returntostart2 = new ReturnToStart(), returntostart3 = new ReturnToStart(), returntostart4 = new ReturnToStart(), returntostart5 = new ReturnToStart(), returntostart6 = new ReturnToStart(), returntostart7 = new ReturnToStart(), returntostart8 = new ReturnToStart();
        addObject(hedgehog,99,487);
        addObject(ground9,102,549);
        addObject(ground10,701,254);
        addObject(ground,85,675);
        addObject(ground2,245,675);
        addObject(ground3,405,675);
        addObject(ground4,565,675);
        addObject(ground5,725,675);
        addObject(ground6,885,675);
        addObject(ground7,1045,675);
        addObject(ground8,1205,675);
        addObject(shortground,323,457);
        addObject(shortground2,179,367);
        addObject(shortground3,48,289);
        addObject(shortground4,359,254);
        addObject(shortground5,509,321);
        addObject(shortground6,196,193);
        addObject(shortground7,887,190);
        addObject(shortground8,1034,140);
        addObject(shortground9,1135,244);
        addObject(shortground10,1004,459);
        addObject(shortground11,849,471);
        addObject(shortground12,677,469);
        addObject(returntostart,612,444);
        addObject(returntostart2,612,369);
        addObject(returntostart3,645,328);
        addObject(returntostart4,712,320);
        addObject(returntostart5,886,271);
        addObject(returntostart6,778,313);
        addObject(returntostart7,844,305);
        addObject(returntostart8,927,238);
        returntostart3.turn(90);
        returntostart4.turn(90);
        returntostart6.turn(90);
        returntostart7.turn(90);
        returntostart8.turn(90);
    }

    private void terrain2()
    {
        Ground ground9 = new Ground(), ground10 = new Ground(), ground11 = new Ground(), ground12 = new Ground();
        ShortGround shortground = new ShortGround(), shortground2 = new ShortGround(), shortground3 = new ShortGround(), shortground4 = new ShortGround(), shortground5 = new ShortGround(), shortground6 = new ShortGround(), shortground7 = new ShortGround(), shortground8 = new ShortGround(), shortground9 = new ShortGround(), shortground10 = new ShortGround(), shortground11 = new ShortGround(), shortground12 = new ShortGround(), shortground13 = new ShortGround();
        ReturnToStart returntostart = new ReturnToStart(), returntostart2 = new ReturnToStart(), returntostart3 = new ReturnToStart(), returntostart4 = new ReturnToStart();
        addObject(hedgehog,1084,493);
        addObject(ground,85,675);
        addObject(ground2,245,675);
        addObject(ground3,405,675);
        addObject(ground4,565,675);
        addObject(ground5,725,675);
        addObject(ground6,885,675);
        addObject(ground7,1045,675);
        addObject(ground8,1205,675);
        addObject(ground9,1083,551);
        addObject(ground10,447,515);
        addObject(ground11,431,157);
        addObject(ground12,865,109);
        addObject(shortground,883,527);
        addObject(shortground2,737,445);
        addObject(shortground3,591,356);
        addObject(shortground4,236,501);
        addObject(shortground5,115,398);
        addObject(shortground6,239,295);
        addObject(shortground7,74,212);
        addObject(shortground8,242,139);
        addObject(shortground9,647,134);
        addObject(shortground10,1088,137);
        addObject(shortground11,1162,296);
        addObject(shortground12,975,351);
        addObject(shortground13,808,277);
        addObject(returntostart,381,541);
        addObject(returntostart2,456,541);
        addObject(returntostart3,531,541);
        addObject(returntostart4,774,140);
        returntostart.turn(90);
        returntostart2.turn(90);
        returntostart3.turn(90);
    }

    private void terrain3()
    {
        addObject(ground,85,675);
        addObject(ground2,245,675);
        addObject(ground3,405,675);
        addObject(ground4,565,675);
        addObject(ground5,725,675);
        addObject(ground6,885,675);
        addObject(ground7,1045,675);
        addObject(ground8,1205,675);
    }
    
    private void prepare()
    {
    }
}
(The player uses to move around)The hedgehog actor class code:
import greenfoot.*;
public class Hedgehog extends Actor
{
    private int frames = 0,  seconds = 0, speed = 0, vSpeed = 0, acceleration = 2, jumpStrength = 20;
    public SimpleActor Nope = new SimpleActor();
    public void act() 
    {
        OnGround();
        checkKeys();
        checkFall();
        if(!getWorld().getObjects(SimpleActor.class).isEmpty())
        {
            if(seconds == 3)
            {
                getWorld().removeObject(getOneObjectAtOffset(600,30,SimpleActor.class));
                frames = 0;
            }
            seconds = (frames += 1/60);
        }
    }
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("Left")||Greenfoot.isKeyDown("a"))
        {
            this.setImage("hedgehog - left.png");
            move(-7);
        }
        if (Greenfoot.isKeyDown("Right")||Greenfoot.isKeyDown("d"))
        {
            this.setImage("hedgehog - right.png");
            move(7);
        }
        if(Greenfoot.isKeyDown("space")) 
        {
            if( OnGround() == true || OnShortGround() == true) 
            {
                jump();
            }
        }
    }
    public void jump()
    {
        vSpeed = -jumpStrength;
        fall();
    }
    public boolean OnGround()
    {
        Actor under = getOneObjectAtOffset (0, 31, Ground.class);
        return under != null; 
    } 
    public boolean OnShortGround()
    {
        Actor under = getOneObjectAtOffset (0, 31, ShortGround.class);
        return under != null; 
    } 
    public void checkFall()
    {
        if(OnGround()||OnShortGround())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
    public void fall()
    {
        setLocation (getX(),getY() + vSpeed);
        vSpeed = vSpeed + acceleration;
    }
}
(the player returns to the starting point if the player touches this)The ReturnToStart actor class code:
import greenfoot.*;
public class ReturnToStart extends Actor
{
    public void act() 
    {
        //to make this invisible
        this.getImage().setTransparency(0);
    }    
}
Super_Hippo Super_Hippo

2018/5/1

#
You can have a method like in your hedgehog class and call that method:
public boolean touchingSimpleActor()
{
    return getOneInterectingObject(SimpleActor.class);
}
Or you check it from the hedgehog's act method and then get the starting point of the current world to return there. Or the world gives the starting point to the hedgehog when it is created.
Lavenger Lavenger

2018/5/1

#
Super_Hippo wrote...
You can have a method like in your hedgehog class and call that method:
public boolean touchingSimpleActor()
{
    return getOneInterectingObject(SimpleActor.class);
}
Or you check it from the hedgehog's act method and then get the starting point of the current world to return there. Or the world gives the starting point to the hedgehog when it is created.
i've tried makng this method in the hedgehog class:
public void Restart (int SX, int SY)
{
    if(this.getOneIntersectingObject(SimpleActor.class) != null)
    {
        this.setLocation(SX,SY);
    }
}
and wrote hedgehog.Restart(x,y); after the addObject(hedgehog,x,y); line in each of the terrain methods Note that x=x coordinate of the hedgehog and y=ycoordinate of the hedgehog But i have no idea how to call the method without reseting the values so I'm figuring that i have to either create a string that states which map in being used and will tell the Restart method which coordinate to use based off which map is being used or use another way (which i have no idea how to do so as of yet)
Lavenger Lavenger

2018/5/1

#
Lavenger wrote...
create a string that states which map in being used and will tell the Restart method which coordinate to use based off which map is being used
oh hey this worked; well idk why am i thinking so hard for these 6 hrs
You need to login to post a reply.