GroupSprite Usage Examples

These aren't *all* the methods in GroupSprite... check out the code in GroupSprite.java for the rest!


Note: To use GroupSprite in your game, right-click (or control-click) on the link above to GroupSprite.java, and save the file. Then, drag the saved file into the package in Eclipse that contains your game's other code files.

GroupSprite lander;

// When you create the group sprite, the shape you give the constructor
// is the shape of the first sprite in the group
lander = new GroupSprite(new Ellipse2D.Double(0, 0, 1, 1));

// You can add a new sprite and give it the shape and offset position
// (from the first sprite's center) in the constructor...
lander.add(new Sprite(new Ellipse2D.Double(0, 0, 1, 1)), new Point2D.Double(30,20));

// ... or you can give the shape and offset seperately
// (the group is 0-indexed, so this is the third sprite in the group)
lander.add();
lander.setShape(2, new Ellipse2D.Double(0, 0, 1, 1));
lander.setOffset(2, new Point2D.Double(10,-40));

// You only need to add the group to the canvas once
canvas.addSprite(lander);

// You can do changes to all the sprites in the group...
lander.setScale(canvas.getWidth() * 0.1);
lander.setLocation(canvas.getWidth() * 0.45, canvas.getHeight() * 0.1); // move the whole group
lander.setColor(Color.BLUE); // make all sprites in the group blue

// ...and you can also do changes to individual sprites in the group
lander.setColor(0, Color.GREEN); // make the first sprite green
lander.setColor(2, Color.RED); // make the third sprite red

lander.scale(0, .5);
lander.scale(1, .7);
lander.scale(2, .9);

lander.setShape(1, new Rectangle(0,0,1,1));

lander.setVisible(0, false);

// The whole group only needs one tracker
landerMover = new LanderTracker(lander.getLocation(), new Point2D.Double(0, canvas.getHeight() * 0.075));
lander.setTracker(landerMover);