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

2020/12/13

reference classes

Patdumas77 Patdumas77

2020/12/13

#
im trying to figure out how to reference the public class play from my public volume up class. i need to control the volume from mySong in public class play from class volume up import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(650, 450, 1); addObject (new Play(),400,200); addObject (new VolumeUp(),200,100); addObject (new VolumeDown(),200,300); } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Play here. * * @author (your name) * @version (a version number or a date) */ public class Play extends Actor { private GreenfootSound mySong; private GreenfootImage img1 = new GreenfootImage("play.jpg"); private GreenfootImage img2 = new GreenfootImage("pause.jpg"); /** * Act - do whatever the Play wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Play() { mySong = new GreenfootSound("fingers.mp3"); } public void act() { if(Greenfoot.mouseClicked(this)){ if(!(mySong.isPlaying())){ mySong.play(); setImage(img2); }else{ mySong.pause(); setImage(img1); } } } public void PlayPause() { } } import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class VolumeUp here. * * @author (your name) * @version (a version number or a date) */ public class VolumeUp extends Actor { /** * Act - do whatever the VolumeUp wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } }
rocket770 rocket770

2020/12/18

#
If your Play Class is an object in the world you can reference it by this: Play play = getWorld().getObjects(Play.class).get(0); And now you should be able to access its attributes with for example play.PlayPause();
danpost danpost

2020/12/18

#
rocket770 wrote...
If your Play Class is an object in the world you can reference it by this: Play play = getWorld().getObjects(Play.class).get(0); And now you should be able to access its attributes with for example play.PlayPause();
Typecasting is required for that assignment to be effective:
1
Play play = (Play)getWorld().getObjects(Play.class).get(0);
You need to login to post a reply.