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

2017/5/12

Shouldn't this work?

Wasupmacuz Wasupmacuz

2017/5/12

#
I have a Lava class that needs to create an enemy if you are close enough to it (using the distance formula). My code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.lang.Math;
/**
 * Write a description of class Lava here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lava extends TileLoader
{
    private int imgPause=0;
    private int imgID=1;
    private int scrllX;
    private int scrllY;
    public double distance=0;
    public Lava(int x, int y)
    {
        scrllX=x;
        scrllY=y;
    }
    
    /**
     * Act - do whatever the Lava wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(scrllX-Planet.scrollX, scrllY-Planet.scrollY);
        imgPause++;
        if(imgPause==40)
        {
            imgPause=0;
            if(imgID==1)
            {
                setImage("lava2.png");
                imgID=2;
            }
            else
            {
                setImage("lava.png");
                imgID=1;
            }
        }
        if(Greenfoot.getRandomNumber(10000)<1)
        {
            int x1=scrllX;
            int x2=Planet.scrollX;
            int y1=scrllY;
            int y2=Planet.scrollY;
            distance = Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
            if(distance<800.0) //the maximum distance from one corner of the visible world to another is approximately 721.11px
            {
                FireLobster fl=new FireLobster(scrllX,scrllY);
                getWorld().addObject(fl,getX(),getY());
            }
        }
    }    
}
doesn't seem to work because the variable "distance" is always equal to 0.0 However I have to same code in another class to get the distance from the mouse pointer here:
MouseInfo mouse = Greenfoot.getMouseInfo();
        if(Greenfoot.mouseClicked(null) && mouse.getButton()==1 && mouse!=null)
        {
            int x1=getX();
            int x2=mouse.getX();
            int y1=getY();
            int y2=mouse.getY();
            double distance = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
            if(distance<26)
            {
                if(canMakePopup)
                {
                    Planet p=(Planet)getWorld();
                    p.popup("Would you like to enchant your weapons?","You found an enchanted bookshelf!",book);
                    canMakePopup=false;
                }
            }
        }
        else
        {
            canMakePopup=true;
        }
Which works flawlessly. Is there any specific reason that the code for Lava isn't working? EDIT: so I just changed the code that makes the enemies so that it creates them when the mouse is so close with this code:
MouseInfo mouse = Greenfoot.getMouseInfo();
       int x1=getX();
            int x2=mouse.getX();
            int y1=getY();
            int y2=mouse.getY();
            distance = Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
            if(distance<52.0) //the maximum distance from one corner of the visible world to another is approximately 721.11px
            {
                FireLobster fl=new FireLobster(scrllX,scrllY);
                getWorld().addObject(fl,getX(),getY());
            }
This works, but I'm still unsure why the other version of the code doesn't work.
danpost danpost

2017/5/12

#
With your initial code, the values of 'scrllX' and 'scrllY' are zero and are never changed. It may be possible that the same may be said for 'scrollX' and 'scrollY' of the Planet class (that would cause your "distance" to always be zero).
Wasupmacuz Wasupmacuz

2017/5/13

#
danpost wrote...
With your initial code, the values of 'scrllX' and 'scrllY' are zero and are never changed. It may be possible that the same may be said for 'scrollX' and 'scrollY' of the Planet class (that would cause your "distance" to always be zero).
The Planet scrollX and scrollY are always almost never 0. This is how it is changed in the Planet world class:
public void act()
    {
        setPaintOrder(Developer.class,Loading.class,Message.class,PlainText.class,Pause.class,StatBar.class,PlayerStats.class,Eat.class,Menu.class,DayToNight.class,Wall.class,WallEnemy.class,FireLobster.class,Player.class,Bookshelf.class,Bed.class,Floor.class,Lava.class,Grass.class);
        if(!Pause.paused)
        {
            if(Player.sleeping==false)
            {
                if(walking==0)
                {
                    walkX=0;
                    walkY=0;
                    Grass.grassX=0;
                    Grass.grassY=0;
                    Player.isWalking=false;
                    if(delayWalkForBooleans==false)
                    {
                        if(Greenfoot.isKeyDown("w"))
                        {
                            if(canWalkUp==true)
                            {
                                walkY=-4;
                                walking=13;
                                walkDirection=0;
                            }
                        }
                        else
                        {
                            if(Greenfoot.isKeyDown("s"))
                            {
                                if(canWalkDown==true)
                                {
                                    walkY=4;
                                    walking=13;
                                    walkDirection=180;
                                }
                            }
                        }
                        if(Greenfoot.isKeyDown("a"))
                        {
                            if(canWalkLeft==true)
                            {
                                walkX=-4;
                                walking=13;
                                walkDirection=270;
                            }
                        }
                        else
                        {
                            if(Greenfoot.isKeyDown("d"))
                            {
                                if(canWalkRight==true)
                                {
                                    walkX=4;
                                    walking=13;
                                    walkDirection=90;
                                }
                            }
                        }
                        if(Greenfoot.isKeyDown("w") && Greenfoot.isKeyDown("d"))
                        {
                            walkDirection=45;
                            if(!canWalkNorthEast)
                            {
                                walkX=0;
                            }
                        }
                        if(Greenfoot.isKeyDown("w") && Greenfoot.isKeyDown("a"))
                        {
                            walkDirection=315;
                            if(!canWalkNorthWest)
                            {
                                walkX=0;
                            }
                        }
                        if(Greenfoot.isKeyDown("s") && Greenfoot.isKeyDown("d"))
                        {
                            walkDirection=135;
                            if(!canWalkSouthEast)
                            {
                                walkX=0;
                            }
                        }
                        if(Greenfoot.isKeyDown("s") && Greenfoot.isKeyDown("a"))
                        {
                            walkDirection=225;
                            if(!canWalkSouthWest)
                            {
                                walkX=0;
                            }
                        }
                    }
                    else
                    {
                        delayWalkForBooleans=false;
                    }
                }
                else
                {
                    walk(walkX,walkY);
                    Grass.grassX += walkX;
                    Grass.grassY += walkY;
                    delayWalkForBooleans=true;
                    walking--;
                }
            }
            changeTime();
        }
        else
        {
            if(walking>0)
            {
                scrollX=scrollX+(walking*walkX);
                scrollY=scrollY+(walking*walkY);
                walking=0;
            }
        }
        if(Message.numberOfMessages==0)
        {
            Message.messageCount=0;
            Message.messagesDeleted=0;
        }
    }
    
    /**
     * moves the player across the world
     */
    public void walk(int x, int y)
    {
        scrollX=scrollX+x;
        scrollY=scrollY+y;
        Player.isWalking=true;
        Player.walk(walking,walkDirection);
    }
Then, the lava class is made by
Lava lava=new Lava(int x, int y);
The lava is put into the correct place with
setLocation(scrllX-Planet.scrollX, scrllY-Planet.scrollY);
And I see the lava is put into the correct place so I know that they do not equal zero. Any other ideas Dan? I just changed the code (just in case I was wrong and they equal zero for some reason) to:
if(Greenfoot.getRandomNumber(10000)<1)
        {
            int x1=getX();
            int x2=312; //X location of main actor
            int y1=getY();
            int y2=208; //Y location of main actor
            distance = Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
            if(distance<800.0) //the maximum distance from one corner of the visible world to another is approximately 721.11px
            {
                FireLobster fl=new FireLobster(scrllX,scrllY);
                getWorld().addObject(fl,getX(),getY());
            }
        }
but this still does not work.
Wasupmacuz Wasupmacuz

2017/5/13

#
WOW! Stupid mistake. I always pause the scenario to check the distance. but the distance variable was not set to anything beforehand (because it is inside the "if(Greenfoot.getRandomNumber(10000)<1)" statement) *facepalm*. Sorry for taking up your time. For my own clarity the code is now:
int x1=getX();
int x2=312;
int y1=getY();
int y2=208;
distance = Math.sqrt(Math.pow(x2-x1,2) + Math.pow(y2-y1,2));
if(Greenfoot.getRandomNumber(10000)<1)
{
    if(distance<800.0) //the maximum distance from one corner of the visible world to another is approximately 721.11px
    {
        FireLobster fl=new FireLobster(scrllX,scrllY);
        getWorld().addObject(fl,getX(),getY());
    }
}
You need to login to post a reply.