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

2016/12/23

how to get changeX and changeY

1
2
brothermic brothermic

2016/12/23

#
hello, how to calculate how many X you moved and how many Y you moved? i need to know the changeX and changeY for putting it as parameters into a function:
private void move()
    {
        if(Greenfoot.isKeyDown("up")){
            move(3);
        }
        if(Greenfoot.isKeyDown("down")){
            move(-3);
        }
        if(Greenfoot.isKeyDown("right")){
            turn(4);
        }
        if(Greenfoot.isKeyDown("left")){
            turn(-4);
        }
    }
that was my move and next is the function i need it for:
public void shiftScreen(int changeX, int changeY)
    {
        leftBound += changeX;
        rightBound += changeX;
        if(leftBound < 0){
            leftBound = 0;
            rightBound = getWidth();
        }
        else if(rightBound > MAPWIDTH){
                rightBound = MAPWIDTH;
                leftBound = MAPWIDTH - getWidth();
            }
        
        topBound -= changeY;
        bottomBound -=changeY;
        if (topBound < 0){
            topBound = 0;
            bottomBound = getHeight();
        }
        else if(bottomBound > MAPHEIGHT){
            bottomBound = MAPHEIGHT;
            topBound = MAPHEIGHT - getHeight();
        }
        update();
    }
the code to shiftscreen is in the worldclaas while the code for move is in an actor.
danpost danpost

2016/12/23

#
If you keep a reference to the actor in your World subclass:
private Actor player;
assigned a Player object that is added into the world in the class constructor, then you can simply determine how much it moves in the act method with:
int changeX = player.getX()-getWidth()/2;
int changeY = player.getY()-getHeight()/2;
You may want to take a look at my Scrolling Tutorial scenario for help on this.
brothermic brothermic

2016/12/27

#
i dont see how i get the value of the difference between my formal x-y position and my current position? meaby i explained bad let me rephrase. in the mean while following code is working:
public void act() 
    {
        shootMissile();
        shootBullet();
        placeLandmine1();
        reloadDelayCount++;
        missileReloadDelayCount++;
        landMineReloadDelayCount++;
        if(Greenfoot.isKeyDown("left")){
           setLocationWithScroll(getX() - 5, getY());
        }
        if(Greenfoot.isKeyDown("right")){
            setLocationWithScroll(getX() + 5, getY());
        }
    }










 public void setLocationWithScroll(int newX, int newY)
    {
        MyWorld world = (MyWorld) getWorld();
        int xShift = newX - getX();
        int yShift = newY - getY();
        world.shiftScreen(xShift , yShift);
so then my screen shift without problems when i move. however i got this from a tutorial and i just dont wanna move only up down left and right. The thing is that i wanna move forwards and backwards with my up and down arrow and with the left arrow and the right arrow i wanna do a turn. its essential because i want o be able to turn towards my enemys. like this:
 private void move()
    {
        if(Greenfoot.isKeyDown("up")){
            move(3);
        }
        if(Greenfoot.isKeyDown("down")){
            move(-3);
        }
        if(Greenfoot.isKeyDown("right")){
            turn(4);
        }
        if(Greenfoot.isKeyDown("left")){
            turn(-4);
        }
so i tried few little things but my tank is moving quicker then my screen. And i am not sure anymore how to do it. this was my tryout:
public void shiftScreenWhileMoving()
    {
        int currentX = getX();
        int currentY = getY();
        if(Greenfoot.isKeyDown("up")){
            move(3);
            int changeX = getX() - currentX;
            int changeY = currentY - getY();
            MyWorld myWorld = (MyWorld) getWorld();
            
            myWorld.shiftScreen(changeX,changeY);
        }
        if(Greenfoot.isKeyDown("down")){
            move(-3);
            int changeX = getX() - currentX;
            int changeY = currentY - getY();
            MyWorld myWorld = (MyWorld) getWorld();
            
            myWorld.shiftScreen(changeX,changeY);
        }
    }
but this doesnt work at all. btw happy christmas! and thanks for your time!
danpost danpost

2016/12/27

#
Please show your MyWorld class code and specify which actor is the main one (the one to code above is placed in).
brothermic brothermic

2016/12/27

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;
import java.util.ArrayList;

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private Tank theTank;
    private static final Map map = new Map();
    private static final GreenfootImage mapImg = map.getImage();;
    private static final int MAP_IMG_WIDTH = mapImg.getWidth();
    private static final int MAP_IMG_HEIGHT = mapImg.getHeight();
    private static final Wall wallTemplate = new Wall(0,0);
    private static final GreenfootImage myWallImage = wallTemplate.getImage();
    private static final int WALL_HEIGHT = myWallImage.getHeight();
    private static final int WALL_WIDTH = myWallImage.getWidth();
    private static final int MAPWIDTH =  MAP_IMG_WIDTH * WALL_WIDTH;
    private static final int MAPHEIGHT = MAP_IMG_HEIGHT * WALL_HEIGHT;
    private List<Wall> theWalls;
    private int leftBound;
    private int rightBound;
    private int bottomBound;
    private int topBound;

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(850, 850, 1); 
        theTank = new Tank(); 
        addObject(theTank,20, 820);
        //map = new Map();
        //mapImg = map.getImage();
        //wallTemplate = new Wall(0,0);
        //myWallImage = wallTemplate.getImage();
        theWalls = new ArrayList<Wall>();
        leftBound = 0;
        rightBound = getWidth();
        bottomBound = MAPHEIGHT;
        topBound = MAPHEIGHT - getHeight();
        makeMap();
        update();
    }

    
    public void makeMap()
    {
        for(int y = 0;y < MAP_IMG_HEIGHT; y++){
            for(int x = 0;x < MAP_IMG_WIDTH; x++){
                int colorRGB = mapImg.getColorAt(x,y).getRGB();
                if (colorRGB == Color.BLACK.getRGB()){
                    int mapX= x * WALL_WIDTH + WALL_WIDTH/2;
                    int mapY= y * WALL_HEIGHT + WALL_HEIGHT/2;
                    theWalls.add(new Wall(mapX,mapY));
                }
            }
        }
    }

    public void shiftScreen(int changeX, int changeY)
    {
        leftBound += changeX;
        rightBound += changeX;
        if(leftBound < 0){
            leftBound = 0;
            rightBound = getWidth();
        }
        else if(rightBound > MAPWIDTH){
            rightBound = MAPWIDTH;
            leftBound = MAPWIDTH - getWidth();
        }

        topBound -= changeY;
        bottomBound -=changeY;
        if (topBound < 0){
            topBound = 0;
            bottomBound = getHeight();
        }
        else if(bottomBound > MAPHEIGHT){
            bottomBound = MAPHEIGHT;
            topBound = MAPHEIGHT - getHeight();
        }
        update();
    }

    public void update()
    {
        Wall thisWall;
        int thisWallX;
        int thisWallY;
        int screenX;
        int screenY;
        for(int i = 0; i < theWalls.size(); i++){
            thisWall = theWalls.get(i);
            thisWallX = thisWall.getMapX();
            thisWallY = thisWall.getMapY();
            if(thisWallX >= leftBound && thisWallX <= rightBound && thisWallY >= topBound && thisWallY <= bottomBound){
                screenX = thisWallX - leftBound;
                screenY = thisWallY - topBound;
                if(thisWall.getWorld() == null){
                    addObject(thisWall, screenX , screenY);
                }
                else{
                    thisWall.setLocation(screenX , screenY);
                }
            }
            else{
                if(thisWall.getWorld() != null){
                    removeObject(thisWall);
                }
            }
        }
    }

    public Tank getTheTank()
    {
        return theTank;
    }
}
that was my world class the actor that i wanna move and scroll the screen while moving is 'Tank'. i made an image in paint. for that image i get the rgb black and on every black pixel i put a brick wall (in worldclass). the code for moving the tank and scrolling the screen in the meanwhile i wanted to pu that in the classe tank. I will paste the code of tank aswell here:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tank extends Actor
{
    private int gunReloadTime;  // time it takes to reload gun
    private int reloadDelayCount; //time since last shot fired
    private int missileReloadTime; //time to reload missile
    private int missileReloadDelayCount; // time since last missile fired
    private int landMineRelaodTime;
    private int landMineReloadDelayCount;
    
    public Tank()
    {
        gunReloadTime = 10;
        reloadDelayCount = 0;
        missileReloadTime = 100;
        missileReloadDelayCount = 0;
        landMineRelaodTime = 500;
        landMineReloadDelayCount = 0;
    }
    /**
     * Act - do whatever the Tank wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    
    public void act() 
    {
        shootMissile();
        shootBullet();
        placeLandmine1();
        reloadDelayCount++;
        missileReloadDelayCount++;
        landMineReloadDelayCount++;
        if(Greenfoot.isKeyDown("left")){
           setLocationWithScroll(getX() - 5, getY());
        }
        if(Greenfoot.isKeyDown("right")){
            setLocationWithScroll(getX() + 5, getY());
        }
    }
    
    private void move()
    {
        if(Greenfoot.isKeyDown("up")){
            move(3);
        }
        if(Greenfoot.isKeyDown("down")){
            move(-3);
        }
        if(Greenfoot.isKeyDown("right")){
            turn(4);
        }
        if(Greenfoot.isKeyDown("left")){
            turn(-4);
        }
    }
    
    public void setLocationWithScroll(int newX, int newY)
    {
        MyWorld world = (MyWorld) getWorld();
        int xShift = newX - getX();
        int yShift = newY - getY();
        world.shiftScreen(xShift , yShift);
    }
    
    /*public void shiftScreenWhileMoving()
    {
        int currentX = getX();
        int currentY = getY();
        if(Greenfoot.isKeyDown("up")){
            move(3);
            int changeX = getX() - currentX;
            int changeY = currentY - getY();
            MyWorld myWorld = (MyWorld) getWorld();
            
            myWorld.shiftScreen(changeX,changeY);
        }
        if(Greenfoot.isKeyDown("down")){
            move(-3);
            int changeX = getX() - currentX;
            int changeY = currentY - getY();
            MyWorld myWorld = (MyWorld) getWorld();
            
            myWorld.shiftScreen(changeX,changeY);
        }
    }*/
    
    private void shootBullet()
    {
        if(Greenfoot.isKeyDown("space")){
            fireBullet();
        }
    }
    
    private void shootMissile()
    {
        if(Greenfoot.isKeyDown("v")){
            fireMissile();
        }
    }
    
    private void placeLandmine1()
    {
        if(Greenfoot.isKeyDown("b")){
            placeLandMine();
        }
    }
    
    public void fireBullet()
    {
        if (reloadDelayCount >= gunReloadTime){
            getWorld().addObject(new Bullet(getRotation()),getX(),getY());
            reloadDelayCount = 0;
        }
    }
    
    public void fireMissile()
    {
        if(missileReloadDelayCount >= missileReloadTime){
            getWorld().addObject(new Missile(getRotation()), getX(), getY());
            missileReloadDelayCount = 0;
        }
    }
    
    public void placeLandMine()
    {
        if(landMineReloadDelayCount >= landMineRelaodTime){
            getWorld().addObject(new Landmine(),getX(),getY());
            landMineReloadDelayCount = 0;
        }
    }
}
brothermic brothermic

2016/12/27

#
the move() function should not be active in this code cause i was testing the tutorialcode 'setlocationwithscroll'. but yeah thats not what i wanna use since i need to be able to turn and rotate 360 degrees
danpost danpost

2016/12/27

#
So, this is a top-down map-limited scrolling world. I gather the tank can roam around the viewport to about 50 pixels from an edge and then scroll to limit before proceeding to the edge. I say this because of the initial placement of the tank when added into the world. However, with the scrolling in the tank class, it appears you want the tank to remain at the bottom left corner of the viewport (which does not seem quite right). Before continuing, you should verify my findings.
danpost danpost

2016/12/27

#
I noticed that the 'move' method is not being called at all. With the code shown above, it is just a map-limited side-scroller (there is no turning of the tank). I would suggest removing lines 41 through 46 of the Tank class and add the following line at the beginning of the act method (insert at line 35):
move();
We can work on the scrolling when I get the following information: * type of scroller (view-type, limits of scrolling in both horizontal and vertical * limits of tank position in viewport (maintain a specific position -- specify center-screen or some other location; or roaming to what limits)
brothermic brothermic

2016/12/27

#
hey dan, well, the tank was on the left bottomcorner position just because i was running some tests when i copied code. yes indeed i think i gonna let the tank start somewhere in the middle of the screen. the idea original is to let the screen shift to other parts of the map when i come in a certain distance from the edge of the screen. depends. meaby like a 150 pixels from edge of the screen but the tank must be able drive to the end of the map where will be walls. ofcourse the scrolling world will be limited to the size of the map. its a limited scrollingworld as you can see in the code of myworld class. not sure what you mean by viewtype. So are you suggesting to not touch the code of the tank (the move function) and solve the shiftproblem another way? so for example when i reach a certain xy-coordinates to let the screen shift?
danpost danpost

2016/12/28

#
brothermic wrote...
not sure what you mean by viewtype.
I am quite sure that you are trying a top-down view (view from above).
So are you suggesting to not touch the code of the tank (the move function) and solve the shiftproblem another way? so for example when i reach a certain xy-coordinates to let the screen shift?
It appears you may have understood. So, with a call to 'move' at the beginning of the act method of the tank and then a call to 'scroll' at the end of the act method of the tank, you could have something like this:
private void scroll()
{
    int dx = 0;
    int dy = 0;
    if (getX() < 150) dx = getX()-150;
    if (getX() > getWorld().getWidth()-150) dx = getX()-getWorld().getWidth()-150;
    if (getY() < 150) dy = getY()-150;
    if (getY() > getWorld().getHeight()-150) dy = getY()-getWorld().getHeight()-150;
    if (dx == 0 && dy == 0) return; 
    ((MyWorld)getWorld()).shiftscreen(dx, dy);
}
brothermic brothermic

2016/12/28

#
yeah that seems like it must work. however when i move my tank to the edges my screen doesnt move at all? not sure why it doesnt move :( everything looks fine.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tank extends Actor
{
    private int gunReloadTime;  // time it takes to reload gun
    private int reloadDelayCount; //time since last shot fired
    private int missileReloadTime; //time to reload missile
    private int missileReloadDelayCount; // time since last missile fired
    private int landMineRelaodTime;
    private int landMineReloadDelayCount;
    private int hp; // healthpoints

    public Tank()
    {

        gunReloadTime = 10;
        reloadDelayCount = 0;
        missileReloadTime = 100;
        missileReloadDelayCount = 0;
        landMineRelaodTime = 500;
        landMineReloadDelayCount = 0;
        hp = 1000;
    }

    /**
     * Act - do whatever the Tank wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public void act() 
    {
        move();
        shootMissile();
        shootBullet();
        placeLandmine1();
        reloadDelayCount++;
        missileReloadDelayCount++;
        landMineReloadDelayCount++;
        scroll();
        die();

    }

    private void move()
    {
        if(Greenfoot.isKeyDown("up")){
            move(3);
        }
        if(Greenfoot.isKeyDown("down")){
            move(-3);
        }
        if(Greenfoot.isKeyDown("right")){
            turn(4);
        }
        if(Greenfoot.isKeyDown("left")){
            turn(-4);
        }
    }

    private void scroll()
    {
        int dx = 0;
        int dy = 0;
        if (getX() < 150){
            dx = getX()-150;
        }
        if (getX() > getWorld().getWidth()-150) {
            dx = getX()-getWorld().getWidth()-150;
        }
        if (getY() < 150){
            dy = getY()-150;
        }
        if (getY() > getWorld().getHeight()-150){
            dy = getY()-getWorld().getHeight()-150;
        }
        if (dx == 0 && dy == 0){ 
            return;
        }

            ((MyWorld)getWorld()).shiftScreen(dx, dy);
    }

    /*public void shiftScreenWhileMoving()
    {
    int currentX = getX();
    int currentY = getY();
    if(Greenfoot.isKeyDown("up")){
    move(3);
    int changeX = getX() - currentX;
    int changeY = currentY - getY();
    MyWorld myWorld = (MyWorld) getWorld();

    myWorld.shiftScreen(changeX,changeY);
    }
    if(Greenfoot.isKeyDown("down")){
    move(-3);
    int changeX = getX() - currentX;
    int changeY = currentY - getY();
    MyWorld myWorld = (MyWorld) getWorld();

    myWorld.shiftScreen(changeX,changeY);
    }
    }*/

    private void shootBullet()
    {
        if(Greenfoot.isKeyDown("space")){
            fireBullet();
        }
    }

    private void shootMissile()
    {
        if(Greenfoot.isKeyDown("v")){
            fireMissile();
        }
    }

    private void placeLandmine1()
    {
        if(Greenfoot.isKeyDown("b")){
            placeLandMine();
        }
    }

    public void fireBullet()
    {
        if (reloadDelayCount >= gunReloadTime){
            getWorld().addObject(new Bullet(getRotation()),getX(),getY());
            reloadDelayCount = 0;
        }
    }

    public void fireMissile()
    {
        if(missileReloadDelayCount >= missileReloadTime){
            getWorld().addObject(new Missile(getRotation()), getX(), getY());
            missileReloadDelayCount = 0;
        }
    }

    public void placeLandMine()
    {
        if(landMineReloadDelayCount >= landMineRelaodTime){
            getWorld().addObject(new Landmine(),getX(),getY());
            landMineReloadDelayCount = 0;
        }
    }

    public void die()
    {
        if(hp <= 0){
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2016/12/28

#
Oh my! I now see what you are trying to do with the walls. Adding and removing any actor because they reach the edge of the screen is not necessary. You can unbound the world so that actors can remain in the world but outside the view window. Remove the 'theWalls' list and the 'update' method. All you need to do is keep track of how far you have scrolled horizontally and vertically, maintain its limits and move all actors the same direction and distance when scrolling. Have you looked at my tutorial? See the World class API documentation and look at the possible World constructors available.
brothermic brothermic

2016/12/29

#
indeed the list wont be needed cause instead of putting things in memory(list) and add the walls when they should be visible i can just add all the walls already on the map. not sure how to keep track of how far i scrolled by moving my tank and how i must move al actors in the same direction. i clicked on the tutorial link but cant open anything besides going to the code in the discussion. extends object? this is an actor right? Is there anything i can keep from my code? find i pretty difficult to see what i can keep from my code and decide or see what i can use from your code? also not sure what the image is you have to give in contructor as parameter. Not relly seeing it anymore and my exam is comming closer :( losing so much time with this scrolling-project
brothermic brothermic

2016/12/29

#
as you can see i cant really open any tutorial
brothermic brothermic

2016/12/29

#
http://nl.tinypic.com/r/mj0fax/9
There are more replies on the next page.
1
2