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

2018/5/4

How to make an actor move a certain amount

1
2
OfficerRiot OfficerRiot

2018/5/4

#
I have a boss actor and I only want it to move a certain amount. I've looked everywhere but nothing I find nothing that helps here is the code: /** * Write a description of class WalkerBoss here. * * @author (your name) * @version (a version number or a date) */ public class WalkerBoss extends Actor { private int frame; private GreenfootImage walker1 = new GreenfootImage("WalkerBoss1.png"); private GreenfootImage walker2 = new GreenfootImage("WalkerBoss2.png"); private GreenfootImage walker3 = new GreenfootImage("WalkerBoss3.png"); private GreenfootImage walker4 = new GreenfootImage("WalkerBoss4.png"); private GreenfootImage walker5 = new GreenfootImage("WalkerBoss5.png"); private GreenfootImage walker6 = new GreenfootImage("Walkerboss6.png"); private GreenfootImage walker7 = new GreenfootImage("WalkerBoss7.png"); private GreenfootImage walker8 = new GreenfootImage("WalkerBoss8.png"); private GreenfootImage walker9 = new GreenfootImage("WalkerBoss9.png"); private GreenfootImage walker10 = new GreenfootImage("WalkerBoss10.png"); private GreenfootImage walker11 = new GreenfootImage("WalkerBoss11.png"); private GreenfootImage walker12 = new GreenfootImage("WalkerBoss12.png"); private GreenfootImage walker13 = new GreenfootImage("WalkerBoss13.png"); private GreenfootImage walker14 = new GreenfootImage("WalkerBoss14.png"); private GreenfootImage walker15 = new GreenfootImage("WalkerBoss15.png"); private GreenfootImage walker16 = new GreenfootImage("Walkerboss16.png"); private int imageCounter; private int walkerspeed = -8; private int speed = 0; public static GreenfootSound AT_AT_Walk = new GreenfootSound("AT AT sound.mp3"); int collisionCountdown = 0; private int timer = 0; public void act() { bossmove(); AT_AT_Walk.playLoop(); } private void addTimer(){ timer++; } public void bossmove() { imageCounter = (++imageCounter)%15; // change '2' higher to slow down animation if (imageCounter == 0) switchImage(); } public void move() { bossmove(); } public void switchImage() { move(walkerspeed); frame++; switch (frame){ case 1: setImage(walker1); break; case 2: setImage(walker2); break; case 3: setImage(walker3); break; case 4: setImage(walker4); break; case 5: setImage(walker5); break; case 6: setImage(walker6); break; case 7: setImage(walker7); break; case 8: setImage(walker8); break; case 9: setImage(walker9); break; case 10: setImage(walker10); break; case 11: setImage(walker11); break; case 12: setImage(walker12); break; case 13: setImage(walker13); break; case 14: setImage(walker14); break; case 15: setImage(walker15); break; case 16: setImage(walker16); break; case 17: frame = 0; } } private void checkForJump() { } /** * Hit this asteroid dealing the given amount of damage. */ public void hit(int damage) { } }
Miguel.Valdez Miguel.Valdez

2018/5/4

#
public void move()
{
    move(5)
}
That's a simple method for an object to move... Which then you can get much more complex with it. In regards to what direction, or if you want it to move toward when a key is pressed. Sorry for errors, on my phone.
danpost danpost

2018/5/4

#
Use something like:
if (getX() > 300) move(walkerspeed); // addjust location as needed
for the first line of the switchImage method.
OfficerRiot OfficerRiot

2018/5/6

#
danpost wrote...
Use something like:
if (getX() > 300) move(walkerspeed); // addjust location as needed
for the first line of the switchImage method.
Ok, I have one problem with that and that is the switch image method doesn't stop with the movement. How would I fix this?
danpost danpost

2018/5/6

#
OfficerRiot wrote...
<< Quote Omitted >> Ok, I have one problem with that and that is the switch image method doesn't stop with the movement. How would I fix this?
One way is to change it to:
if (getX() < 300) return;
move(walkerspeed);
OfficerRiot OfficerRiot

2018/5/6

#
danpost wrote...
OfficerRiot wrote...
<< Quote Omitted >> Ok, I have one problem with that and that is the switch image method doesn't stop with the movement. How would I fix this?
One way is to change it to:
if (getX() < 300) return;
move(walkerspeed);
Ok, that worked but there is one more thing I can't seem to get, the walking sound effect also doesn't stop with it any way I could get that to stop?
danpost danpost

2018/5/6

#
OfficerRiot wrote...
<< Quote Omitted >> Ok, that worked but there is one more thing I can't seem to get, the walking sound effect also doesn't stop with it any way I could get that to stop?
Add a third line to the above:
if (getX() < 300) AT_AT_Walk.stop();
OfficerRiot OfficerRiot

2018/5/6

#
danpost wrote...
OfficerRiot wrote...
<< Quote Omitted >> Ok, that worked but there is one more thing I can't seem to get, the walking sound effect also doesn't stop with it any way I could get that to stop?
Add a third line to the above:
if (getX() < 300) AT_AT_Walk.stop();
Ok, I did that, but it stops and then starts to play again. It doesn't stop completely
danpost danpost

2018/5/7

#
OfficerRiot wrote...
Ok, I did that, but it stops and then starts to play again. It doesn't stop completely
That is because you start it in your act method. Start it in a constructor block instead.
OfficerRiot OfficerRiot

2018/5/7

#
danpost wrote...
OfficerRiot wrote...
Ok, I did that, but it stops and then starts to play again. It doesn't stop completely
That is because you start it in your act method. Start it in a constructor block instead.
I did that the only thing that it did was that it stopped longer than played again. Maybe im doing it wrong?
danpost danpost

2018/5/7

#
OfficerRiot wrote...
I did that the only thing that it did was that it stopped longer than played again. Maybe im doing it wrong?
If you think so, then show what you tried..
OfficerRiot OfficerRiot

2018/5/7

#
public void WalkerEffect() { AT_AT_Walk.play(); } That is the code I added.
danpost danpost

2018/5/7

#
Remove the word void to make it a constructor and not a regular method. Also, it should be called WalkerBoss (using the name of the class).
OfficerRiot OfficerRiot

2018/5/7

#
danpost wrote...
Remove the word void to make it a constructor and not a regular method. Also, it should be called WalkerBoss (using the name of the class).
Ok that worked but now it plays before i click run
danpost danpost

2018/5/7

#
OfficerRiot wrote...
Ok that worked but now it plays before i click run
Alright. Time to go back a bit. Remove the constructor and start the sound in the act method using this line:
if (AT_AT_Walk != null) AT_AT_Walk.playLoop();
Then do the following:
// change this line
if (getX() < 300) AT_AT_Walk.stop();

// to this
if (getX() < 300)
{
    AT_AT_Walk.stop();
    AT_AT_Walk = null;
}
There are more replies on the next page.
1
2