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

Report as inappropriate.

davmac
davmac presents ...

2011/1/14

tut-access-p1

This scenario is part of a tutorial about accessing objects from another. See the tutorial here:
http://www.greenfoot.org/doc/howto-1

--------

This is a simple game. Press space bar to fire a shot at the asteroids which go past overhead. There is a delay between each shot so you must aim carefully.

The counter at the bottom left is meant to be a score. It isn't functional. Making it work is what the tutorial is all about!

49822 views / 49 in the last 7 days

4 votes | 0 in the last 7 days

Tags: game with-source howto

open in greenfoot
Your browser is ignoring the <APPLET> tag.
FEearlobeFEearlobe

2013/7/4

I<3disting
davecoolkiddavecoolkid

2013/8/27

Could you explain to me what this line of code does: Space spaceWorld = (Space) getWorld(); What do the parentheses mean when you put them around "(Space)"?
danpostdanpost

2013/8/27

'getWorld' return a World object (not particularly a 'Space' object; though it could be one). That is what '(Space)' does. It makes it a 'Space' object. This is called 'typecasting' and it informs the system to look in the 'Space' class for any methods or fields referenced with the World object. Any methods or fields that are declared in the 'Space' class would not be found otherwise.
davmacdavmac

2013/8/27

davecoolkid: as danpost says 'getWorld()' returns a 'World', but in the line of code you specify the variable is of type 'Space'. The compiler doesn't let you assign just a World to a Space variable (even though the World *might* be a Space). In this case we know that the world definitely *is* a space, so we use this type cast to tell the compiler that it's ok. A simple experiment you can do is to remove the type cast (the "(Space)" part) and try to compile. What happens?
davecoolkiddavecoolkid

2013/8/27

Thanks danpost and davmac for the clarification helped me out a lot.
CubeGamer6CubeGamer6

2017/4/24

Geez, i made a game myself much like this (as in space and stuff) and what suprised me after checking this page is that the space icon that is used here is IDENTICAL. It doesn't help either that the theme is much the same too. I'll just hope my teacher will believe that it is still original XD. Well, atleast i have a fancy movement engine.
marcriemermarcriemer

2019/10/7

Might I humbly suggest an alteration to the code in the Space class: addObject(new Counter(), 40, 340); this needs changing to: addObject(theCounter, 40, 340);
marcriemermarcriemer

2019/10/7

Otherwise, great tutorial...Thanks
marcriemermarcriemer

2019/10/7

private Counter theCounter; /** * Constructor for objects of class Space. */ public Space() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); theCounter = new Counter(); addObject(new Rocket(), 300, 340); addObject(theCounter, 40, 340); }
Super_HippoSuper_Hippo

2019/10/8

See the description of the scenario: “The counter at the bottom left is meant to be a score. It isn't functional. Making it work is what the tutorial is all about!”

See all comments

Want to leave a comment? You must first log in.

Who likes this?

data17 tehbestcoder Antonio wwwayne