Hello!
Was wondering if anyone has a solution to Level 8 in this lesson.
I have written this so far:
var ghost = createSprite(200,300);
ghost.setAnimation(“ghost”);
function draw() {
// Wrap the movement lines in two conditionals
if (World.mouseX >= 200) {
ghost.x = ghost.x + 3;
//chick.x = 350;
}
if ((World.mouseX) < 200) {
ghost.x = ghost.x - 3;
}
drawSprites();
Need help, so the ghost does not move when the mouse is not moving.
Thanks in advance!
Usha
Hi @ukolacha,
Let’s try to break down what the question is asking: it says
make the ghost move either left or right depending on the position of the mouse pointer.
So we know that we want the ghost to move right if the mouse is to the right of the ghost, and we want the ghost to move left if the mouse is to the left of the ghost.
We’re also told
- Write at least two if-statements to check for the location of the mouse.
- Make the ghost move in a different direction for each mouse location.
So, what two values can we compare to determine whether the mouse pointer is to the left or right of the ghost sprite? Then, based on that, how will that determine the way the ghost moves?
Reply here if you need further assistance with this!
–Michael K.
Thanks for your prompt response. I was able to debug this last night.
Usha
2 Likes
@mkmietowicz
In the animation above the workspace on Unit 3 lesson 16 level 8B, it appears the ghost does not move when the mouse is directly over the sprite. Is the lesson asking us to use the sprite’s current location to determine whether to move? I checked the help section and it discusses variables. Is there a way to use variables in this example?
Thanks in advance for your help-
@mkmietowicz
Disregard. I figured it out:
if (World.mouseX > ghost.x) {
ghost.x = ghost.x + 5;
}
if (World.mouseX < ghost.x) {
ghost.x = ghost.x - 5;