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

2012/7/12

Help with Dog Scenario

Wendyb Wendyb

2012/7/12

#
How do I design, in Dog World, a player-controllable Dog object? I know how to create a new Dog object, but am confused as to how to relate it back to a class called MyDog.
limefortheworld limefortheworld

2012/7/14

#
So, the class is MyDog and you are creating a object called dog, right?
//something like this? In the World:

MyDog dog = new MyDog();
super(whatever dimensions you've stated);
addObject(dog, blasted coordinates);
What do you mean relate it back? Do you mean a superclass or a subclass of MyDog?
SPower SPower

2012/7/15

#
That code wouldn't compile: call to super MUST be the first statement. Instead, use this:
super(someints);
MyDog dog = new MyDog();
addObject(dog, x,y);
This would only work btw when this code is in a subclass of World.
Wendyb Wendyb

2012/7/16

#
Thank you for responding. I had already figured it out and SPower, that is what I used to make it work.
steved steved

2012/7/19

#
Instead of using:
MyDog dog = new MyDog();
addObject(dog, x, y)
you could use:
addObject(new MyDog(), x, y);
This makes it alot easier if you are adding alot of objects!
SPower SPower

2012/7/20

#
But it's harder to read if your lines are long.... :)
steved steved

2012/7/23

#
That is true, but it can still help to keep your files at a manageable size.
danpost danpost

2012/7/26

#
@steved, If the dog in question is to be player-controlled, a reference to that dog would be helpful. In fact, the declaration of
MyDog dog = new MyDog();
should probably be in with the world instance fields (that is, declared outside any methods; but still within the class code) with
addObject(dog, x, y);
in the constructor. As an alternative
MyDog dog = null;
could be declared and in the constructor
dog = new MyDog();
addObject(dog, x, y);
but, I prefer the former way for objects that are to be in the world at start-up.
You need to login to post a reply.