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

2012/4/30

Variable Weights Help

1
2
3
JMoller JMoller

2012/4/30

#
hi, i am doing a trebuchet simulation and i am just wondering if there is a way for a player to be able to input weights into the program for the projectile and the counter weight? cheers
danpost danpost

2012/4/30

#
Check out my Color Coordination scenario. It uses my Bar class to adjust values. Maybe you could incorporate the Bar class into your trebuchet simulator, allowing the player to adjust the weight and counter weight values.
JMoller JMoller

2012/5/1

#
thank you
JMoller JMoller

2012/5/1

#
hey dan, after using your bar code i get an error,
 java.lang.ArrayIndexOutOfBoundsException: 2
	at Earth.<init>(Earth.java:26)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at greenfoot.core.Simulation.newInstance(Simulation.java:520)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:406)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:411)
	at greenfoot.core.Simulation.maybePause(Simulation.java:269)
	at greenfoot.core.Simulation.runContent(Simulation.java:201)
	at greenfoot.core.Simulation.run(Simulation.java:194)
do you know what this might be ? cheers jason
danpost danpost

2012/5/1

#
It is in your Earth constructor, line 26. Show the code.
JMoller JMoller

2012/5/1

#
            bars[i] = new Bar(barText[i % 4], "", 63 + 64 * (i < 4 ? i : 1), 255);


JMoller JMoller

2012/5/1

#
the whole class code is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
import java.lang.String;
/**
 * Write a description of class Earth here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Earth extends World
{
 private double gravity = 2;
    static boolean transitioning = false;
    static boolean change = false;
    static final String[] barText = { "Projectile", "CounterWieght"};
    static final int[][] barCols = { { 128,   0,   0, 255 },
                                     {   0, 128,   0, 255 }};
                                  
    Bar[] bars = new Bar[8];
     public Earth()
    {
        super(800, 600, 1);
        for (int i = 0; i < 8; i++) 
        {
            bars[i] = new Bar(barText[i % 4], "", 63 + 64 * (i < 4 ? i : 1), 255);
            bars[i].setSafeColor(new Color(barCols[i % 4][0], barCols[i % 4][1], barCols[i % 4][2], barCols[i % 4][3]));
        }
        for (int i = 0; i < 4; i++) addObject(bars[i], 750, 75 + 25 * i);
        //addObject(new Button(), 750, 200);
        //addObject(new Pad(), 300, 200);
        //updateBackground();
        Greenfoot.start();
    }

 //private Color BackgroundColor = Color.BLACK;
 
    public void act()
    {
        if (!change) return;
        change = false;
        //updateBackground();
        if (transitioning && getObjects(Bar.class).size() < 8) for (int i = 0; i < 4; i++) addObject(bars[4 + i], 750, 275 + 25 * i);
        if (!transitioning && getObjects(Bar.class).size() > 4) for (int i = 0; i < 4; i++) removeObject(bars[4 + i]);
    }

public double getGravity()
{
return gravity;
}


//public static boolean isKeyDown ("1");
public class TrebArm
{

}
public class TrebBase
{
}

public class Trebuchet
{
   TrebArm arm = new TrebArm();
   TrebBase base = new TrebBase();
}
}
danpost danpost

2012/5/1

#
How did you declare bars, and what is the code around the line you provided above (for context)? OK, NVM. I see you posted it!
danpost danpost

2012/5/1

#
The problem is that you only have two elements in your barText array and you are using i % 4 in the line in question, which yeilds { 0, 1, 2, and 3 }. Since there are only two elements, when i becomes 2, this line fails. If you are only to have the two barText Strings, use i % 2 instead.
danpost danpost

2012/5/1

#
You will have the same problem with the colors in line 27.
JMoller JMoller

2012/5/1

#
so all ihave to to is just change all the 4s to 2s ?
danpost danpost

2012/5/1

#
How many bars are you going to be using? (I think you brought across some code that was specific to the scenario you got it from).
JMoller JMoller

2012/5/1

#
im going to use 2 bars
danpost danpost

2012/5/1

#
Line 14: remove Line 20: change 8 to 2 Line 24: change 8 to 2 Line 26: change i % 4 to i and (i < 4 ? i : 1) to i Line 27: change all i % 4 to i Line 29: change 4 to 2 Line 43: remove Line 44: remove This will straighten things out a bit.
JMoller JMoller

2012/5/1

#
hey dan i am getting an error on line 26 after doing what you told me to do,
java.lang.ArrayIndexOutOfBoundsException: 2
	at Earth.<init>(Earth.java:26)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at greenfoot.core.Simulation.newInstance(Simulation.java:520)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:406)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:411)
	at greenfoot.core.Simulation.maybePause(Simulation.java:269)
	at greenfoot.core.Simulation.runContent(Simulation.java:201)
	at greenfoot.core.Simulation.run(Simulation.java:194)
earth class code is
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
import java.lang.String;
/**
 * Write a description of class Earth here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Earth extends World
{
 private double gravity = 2;

 //static boolean transitioning = false;
    static boolean change = false;
    static final String[] barText = { "Projectile", "CounterWieght"};
    static final int[][] barCols = { { 128,   0,   0, 255 },
                                     {   0, 128,   0, 255 }};
                                  
    Bar[] bars = new Bar[2];
     public Earth()
    {
        super( 800, 600, 1);
        for (int i = 0; i; i++) 
        {
            bars[i] = new Bar(barText[i ], "", 63 + 64 * (i), 255);
            bars[i].setSafeColor(new Color(barCols[i ][0], barCols[i ][1], barCols[i][2], barCols[i][3]));
        }
        for (int i = 0; i < 4; i++) addObject(bars[i], 750, 75 + 25 * i);
        //addObject(new Button(), 750, 200);
        //addObject(new Pad(), 300, 200);
        //updateBackground();
        Greenfoot.start();
    }

 //private Color BackgroundColor = Color.BLACK;
 
    public void act()
    {
   //     if (!change) return;
       // change = false;
        //updateBackground();
//if (transitioning && getObjects(Bar.class).size() < 4) for (int i = 0; i < 2; i++) addObject(bars[2 + i], 750, 275 + 25 * i);
      //  if (!transitioning && getObjects(Bar.class).size() > 4) for (int i = 0; i < 2; i++) removeObject(bars[ + i]);
    }

public double getGravity()
{
return gravity;
}


//public static boolean isKeyDown ("1");
public class TrebArm
{

}
public class TrebBase
{
}

public class Trebuchet
{
   TrebArm arm = new TrebArm();
   TrebBase base = new TrebBase();
}
}
There are more replies on the next page.
1
2
3