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

2019/3/12

Get an actor to move down when actor is at edge.

Notted Notted

2019/3/12

#
I want to make an actor move downward by 5 pixels every time that actor hits the edge of the screen. My code right now makes the actor fidget about, not being able to move at all.
private void aliensMovement ()
    {
        move(basicAlienSpeed);
        if ((getX()-gotoX >= 0 && basicAlienSpeed > 0) || (getX()-gotoX <= 0 && basicAlienSpeed < 0))
        {
            basicAlienSpeed = -basicAlienSpeed;
            int highet = getWorld().getHeight();
            gotoY = highet/2 - 5;
        }
    }
danpost danpost

2019/3/12

#
Notted wrote...
I want to make an actor move downward by 5 pixels every time that actor hits the edge of the screen. My code right now makes the actor fidget about, not being able to move at all. << Code Omitted >>
Nowhere in your synopsis do you mention the height of the world (and your collision edges appear to be the left and right ones). Remove line 7 and change line 8 to move the actor down 5 pixels. You may then want to check for bottom edge collision to remove the actor (possibly).
Notted Notted

2019/3/12

#
My world is 800X400 pixels. Even with the admission of line 7, the code still does not work. The actor still fidgets about uncontrollably. This peice of code is likely the casue of the fidgeting, but I'm not sure.
if ((getX()-gotoX >= 0 && basicAlienSpeed > 0) || (getX()-gotoX <= 0 && basicAlienSpeed < 0))
danpost danpost

2019/3/12

#
Notted wrote...
My world is 800X400 pixels. Even with the admission of line 7, the code still does not work. The actor still fidgets about uncontrollably. This peice of code is likely the casue of the fidgeting, but I'm not sure. << Code Omitted >>
Actually, that line appears to be okay -- at least is looks that way. It may have something to do with the use of the fields. To determine if this is the case, more of the class code would need to be provided (isn't that almost always the case). Btw, what did you replace line 8 with?
Notted Notted

2019/3/12

#
This changes the gotoX value.
basicAlienSpeed = -basicAlienSpeed;
This changes the gotoY value.
gotoY = gotoY - 5;
Maybe the change to gotoY is the problem?
/**
     * This is an alien. Shoots at the player and dies.
     * Danspost from greenfoot fourms come to save my ass again.
     */
    private int shotCountDownAlien = 40;
    private int rateOfFireAlien =  1;
    private int basicAlienHealth =  1;
    private int gotoY = 150;
    private int gotoX = 300;
    private int basicAlienSpeed = 4;
    public void act() 
    {
        aliensMovement();
        enemeyShoot();
        playersBulletHitsAlien();
        alienDie();
    } 
    private void enemeyShoot()
    {
       xenoBullet aliensBullet = new xenoBullet();
       int alienY = getY()+getImage().getHeight()/2;
       MyWorld spaceInvWorld = (MyWorld) getWorld();
      if (shotCountDownAlien > 0) 
        {
            shotCountDownAlien -= rateOfFireAlien; 
            
        }
        else if (shotCountDownAlien == 0)
        {
            shotCountDownAlien = 40;
            spaceInvWorld.addObject(aliensBullet, getX(), alienY);
        }
      
    }
    private void playersBulletHitsAlien()
    {
        MyWorld spaceInvWorld = (MyWorld) getWorld();
        boolean playersBulletHasHitAlien = isTouching(worldBullet.class);
        if (playersBulletHasHitAlien)
        { 
            basicAlienHealth = basicAlienHealth - 1;
            removeTouching(worldBullet.class);
        }
    }
    private void alienDie()
    {
        if (basicAlienHealth <= 0)
        {
            if (!getWorld().getObjects(enemyAlien.class).isEmpty())
            {
                
            getWorld().removeObject(this); 
            
            }
        }
    }
    private void aliensMovement ()
    {
        move(basicAlienSpeed);
        if ((getX()-gotoX >= 0 && basicAlienSpeed > 0) || (getX()-gotoX <= 0 && basicAlienSpeed < 0))
        {
            basicAlienSpeed = -basicAlienSpeed;
            gotoY = gotoY - 5;
        }
    }
danpost danpost

2019/3/12

#
The only place I see the field called gotoY being used is on line 63. There is no line of code given that would have the alien move down 5 pixels (line 63 just subtracts 5 from the value of the field). The gotoY field is not needed at all. Replace line 63 with a line that sets the location of the actor 5 pixels down from its current position.
danpost danpost

2019/3/12

#
Notted wrote...
This changes the gotoX value.
basicAlienSpeed = -basicAlienSpeed;
Does it? It changes the direction that the actor is to move; but, the gotoX value is still 300 -- hence, the fidgeting.
Notted Notted

2019/3/13

#
 private void aliensMovement ()
    {
        move(basicAlienSpeed);
        if ((getX()-gotoX >= 0 && basicAlienSpeed > 0) || (getX()-gotoX <= 0 && basicAlienSpeed < 0))
        {
            basicAlienSpeed = -basicAlienSpeed;
            int alienY = getY();
            setLocation(getX(), alienY + 5);
        }
    }
This works for the alien moving downwards, but I'm not sure what to do to change the gotoX field. Decrease it perhaps?
danpost danpost

2019/3/13

#
Notted wrote...
<< Code Omitted >> This works for the alien moving downwards, but I'm not sure what to do to change the gotoX field. Decrease it perhaps?
Well, at this point, you have the actor turning around when the gotoX location is reached. When turning around to face left, to add some distance, gotoX will need to be less than its current value. Likewise, when turning around to face right, a higher value would be required.
Notted Notted

2019/3/14

#
This seems to be working.
private void aliensMovement ()
    {
        move(basicAlienSpeed);
        if ((getX()-gotoX >= 0 && basicAlienSpeed > 0) || (getX()-gotoX <= 0 && basicAlienSpeed < 0))
        {
            basicAlienSpeed = -basicAlienSpeed;
            int alienY = getY();
            setLocation(getX(), alienY + 5);
            int alienWidth = getWorld().getWidth();
            gotoX = alienWidth/2+(50+Greenfoot.getRandomNumber(alienWidth/2-80))*basicAlienSpeed/4;
        }
    }
It's rather noting that most of my code comes from my Kaboom Remake Full rlease. I like to thank you for the debugging on my part. I don't know how could I do it without you.
You need to login to post a reply.