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

2014/3/11

I need help am stuck

Kabaka Kabaka

2014/3/11

#
I want to write a simple code of a car moving left to right with fruits falling when arrows are pressed like in the fb pic below https://m.facebook.com/photo.php?fbid=1389383221334660&id=100007889851716&set=a.1389383288001320.1073741829.100007889851716&source=48
danpost danpost

2014/3/11

#
Where are you stuck at? what code do you have so far?
Kabaka Kabaka

2014/3/11

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Car here. * * @author (your name) * @version (a version number or a date) */ public class Car extends Actor { private static final double WALKING_SPEED = 5.0; public void act () { } public boolean atWorldEdge () { if (getX() < 20 || getX () > getWorld () .getWidth () -20) return true; if (getY() < 20 || getY () > getWorld () .getHeight () - 20) return true; else return false; } public void move () { double angle = Math.toRadians ( getRotation () ); int x = (int) Math.round (getY() + Math.cos (angle) *WALKING_SPEED ); int Y = (int) Math.round (getY() + Math.sin(angle) * WALKING_SPEED ); setLocation (x, Y); } }
Kabaka Kabaka

2014/3/11

#
danpost wrote...
so far am stuck there
Kabaka Kabaka

2014/3/11

#
danpost danpost

2014/3/11

#
Kabaka wrote...
danpost wrote...
so far am stuck there
I did not say that! :{/ If your car is only going to move left and right, then you will not need that 'move' method. I do not see any code where you are getting what keys are pressed. This is what you will be moving to. Also, the 'atWorldEdge' method is not going to be needed (as is) because you need to know which edge (left or right) you are near so you prevent moving further in that direction and can still move back. These checks (for world edge) will be incorporated in your new 'move' method which will: (1) determine direction of movement by what key(s) are down (2) confirm that you can move in that determined direction (3) move if confirmed You can do each step for all keys one by one or you can do all steps for each key (that is up to you). I prefer, with this type movement, to do the latter. An example, for the check for moving left:
if (Greenfoot.isKeyDown("left") && getX()>10) setLocation(getX()-speed, getY());
Remember to either put the movement code in a 'move' method that the 'act' method calls or put it directly in the 'act' method.
Kabaka Kabaka

2014/3/11

#
thanks am gonna try to write it again, but the keys are for the falling fruits not the the car, the car just keeps on runnning the right and then back to left until u press pause.
danpost danpost

2014/3/11

#
You will still need, without the keyboard detection, direction of movement. It will then need to be tracked by an int or boolean field (I use int fields with values of one and negative one which the speed can be multiplied by in the 'setLocation' statement). You will need two checks for changing direction; one is 'direction == -1 && getY() < 10'. Changing direction will just be negating the value of the direction field.
Kabaka Kabaka

2014/3/11

#
I kind of got the car moving left and back right using the animal boolean atWorldEdge but when the car is returning it is upside down idk why, is it because I used 180 degrees?
danpost danpost

2014/3/11

#
Kabaka wrote...
I kind of got the car moving left and back right using the animal boolean atWorldEdge but when the car is returning it is upside down idk why, is it because I used 180 degrees?
Yes.
Kabaka Kabaka

2014/3/11

#
am gonna add another image of the same car so it returns upright to the left but idk how to call it.
danpost danpost

2014/3/11

#
Use one of the Actor class 'setImage' methods. Refer to the documentation.
scottpj3 scottpj3

2014/3/11

#
can someone help me with this iI'm trying to make a fish eat a man i was able to code the fish to eat it but i cant code the man can you please help
danpost danpost

2014/3/11

#
@scottpj3, please start a new discussion for that issue. Also, explain better, in detail, exactly what you are having problems with in the coding of the man. And show some attempted code.
You need to login to post a reply.