I'm trying to make a few class files that could be copy/pasted into one of my sketches (Or someone else's) that would quickly let me make a button with custom/existing functions that could be easily defined in the main sketch, with only minimal other work to the main sketch, and no other modifications to the copy/pasted files besides possibly extending the class besides extending the class with the list of functions to add custom ones.
So basically, I would have a class called Functions which would be defined in the sketch as, lets say, Functions function, which the Button class would grab functions from. It would have a long series of
void ...(...,...,...)s, each of which would do a certain thing, lets say,
void ChangeBackground(
color c), which would be called on in the Button class.
I know how to make a list of functions using a variable in the constructor that would be strained through a series of conditionals to find the right function, but I'm trying to do it where someone could add new functions by simply extending the Functions class/adding new
void ...s to the existing one, instead of modifying the Button. The best way I can think to do this would be putting a variable in the Button class that would be set to a function, which would then be used to call on part of the Functions class which the button of pressed.
Define the Functions class in the sketch (Functions function =
new Functions()) --> Constructor (For example: test =
new Button(...,...,...,function.ChangeBackground(
color(...,...,...)))) --> Have a variable in the class be set to the function --> Call on the function using the variable in a part of the class
I'm not sure if this is possible, and what I've done so far hasn't resulted in anything more than constructor errors, function errors, and the occasional NPE. If anyone has any ideas to help, they would be much appreciated.
Main sketch:
Button test;
Functions function;
void setup() {
imageMode(CENTER);
size(500,500);
test = new Button(width/2,height/2,50,50,loadImage("Musketeer.png"),loadImage("Galleon.png"),"Hello!",function.ChangeBackground(color(255,0,0)));
function = new Functions();
}
void draw() {
test.Display();
}
void mousePressed() {
test.mousePressed();
}
Button class:
class Button {
PVector location; //Location of the button
int xSize,ySize; //Size ofthe button
PImage on,off; //Images for mosue overlap
String type; //Text to be displayed in the button
boolean mouseOver;
void callFuntion;
Button(float x, float y, int xs, int ys, PImage ion, PImage ioff, String t, void a) {
location = new PVector(x,y);
xSize = xs;
ySize = ys;
on = ion;
off = ioff;
type = t;
callFuntion = a;
}
boolean mouseOver(float x, float y, float sizeX, float sizeY) {
This problem has mainly to do with ArrayLists; I really don't know how to use them. I've had to scrap a project that I spent a nice amount of time on because ArrayLists didn't co-operate, and I think I'm scrapping the next one. My inability to work with them has basically stopped me from learning how to code more complicated things. So, before I scrap this project, I was wondering if anyone knew how to fix the problems I made. Basically, I'm stuck on getting the mover (myFirstMover, from the class Mover) to go towards an ArrayList of "targets," (Classes Target and TargetManager) the first one being the mouse (Class MousePointer). Sadly, I can't give you much information on why the problem is happening, other than a nullPointerError in void Behavior in the Mover class, and probably the Mover just not going towards the mouse (If my current luck holds out). Again, I'm sorry for the probably very obvious error.
Anyways, here's the code:
Main sketch:
Mover myFirstMover;
TargetManager tManager;
MousePointer pointer;
void setup() {
frameRate(15);
myFirstMover = new Mover(width/2,height/2);
tManager = new TargetManager();
pointer = new MousePointer();
size(300,300);
}
void draw() {
background(0);
myFirstMover.Display();
myFirstMover.Update();
myFirstMover.Movement();
myFirstMover.Emoticon();
myFirstMover.Behavior();
tManager.Manage();
pointer.Display();
pointer.Update();
}
Mover class:
import java.util.Iterator;
class Mover { //Class for the movers
PVector location,targetLocation; //Location of the mover, and of the target location
float hungerLevel,thirstLevel,happinessLevel; //How humgry, thirsty, and happy the mover is, the higher it gets the more likely the mover is to seek water/food, and if it is high enough, the mover will die
float fear; //Various factors that affect the behavior of the mover
ArrayList standing;
int viewDistance; //Radius of detection for items such as food and water
boolean target; //Whetehr or not the mover is activley moving towards something
int speed,movementTimer,movementDir; //Speed relates to how fast the movement timer increases, and movementTimer/movementDir are used in Movement() to trigger a movement
Mover(float x, float y) { //Constructor
location = new PVector(x,y);
hungerLevel = 5;
thirstLevel = 0;
happinessLevel = 5;
standing = new ArrayList();
viewDistance = 5;
target = false;
speed = round(random(4,8)); //Randomises the speed for each mover
}
void Display() { //Shows the mover
if(happinessLevel<4) { //These conditionals change the color of the mover based on its happiness
stroke(255,0,0);
}
else if(happinessLevel>=4&&happinessLevel<=8) {
stroke(255,255,255);
}
else if(happinessLevel>8) {
stroke(0,255,0);
}
rectMode(CENTER);
rect(location.x,location.y,3,3); //Draws the point
}
void Update() { //Performs various changes in miscelanious values, including managing hunger, thirst, and happiness
movementTimer+=speed;
hungerLevel+=0.0005;
thirstLevel+=0.0012;
if(hungerLevel<2&&happinessLevel<10) {
happinessLevel+=0.006;
}
else if(hungerLevel>5&&happinessLevel>0) {
happinessLevel-=0.01;
}
if(thirstLevel<2&&happinessLevel<10) {
happinessLevel+=0.006;
}
else if(thirstLevel>5&&happinessLevel>0) {
happinessLevel-=0.012;
}
}
void Behavior() { //Behavior regarding targets, other movers etc
boolean Alive() { //True/false for whether or not the mover is alive, if it is false the mover is removed
if(hungerLevel<10&&thirstLevel<10) { //Checks the hunger/thirst levels
return true;
}
else {
return false;
}
}
void Movement() { //Performs movements for the mover
if(movementTimer>50&&target==false) { //Counter for moving it every cerian ammount of time, and making sure it has no target, as this is for randomly moving
movementDir=floor(random(5)); //Randomises the direction
if(movementDir==0) { //Right
location.x++;
movementTimer=0;
}
else if(movementDir==1) { //Down
location.y++;
movementTimer=0;
}
else if(movementDir==2) { //Left
location.x--;
movementTimer=0;
}
else if(movementDir==3) { //Up
location.y--;
movementTimer=0;
}
else { //Mover is resting
movementTimer=-100;
}
}
else if(movementTimer>50&&target==true) {
targetLocation.normalize();
targetLocation.mult(1);
location.add(targetLocation);
}
}
void CheckEdges() { //Bounces the mover back if they try to wander off screen
if(location.x<0) {
location.x++;
}
else if(location.x>width) {
location.x--;
}
if(location.y<0) {
location.y++;
}
else if(location.y>height) {
location.y--;
}
}
void Emoticon() { //DIsplays an "emoticon" above the mover to show why it is happy/unhappy
Quick edit: Based on the location of the null error, my Mover seems to recognize that there is a target available. Also, I commented out the leftover MousePointer things in the main sketch from before I made MousePointer an extension of Target.
Frist of all, sorry for the vague title, it was the closest I could come to describing all my problems. First of all, the rotation doesn't work, (using the ship created in ShipManager.Start()) by which I mean it will rotate, but just in little, seemingly random ways whenever I move the mouse. It should rotate to face the mouse, but I probably messed something up with the translation. Secondly, the movement keys don't work correctly. I can press one key, but the ship just glides off-screen and doesn't respond to any other keys. Lastly, I know this error is known, but on line 133 of the Ship class, (When un-commented, of course) it produces a "Cannot make a static reference to a non-static field" error. Also, any suggestions with any improvements/suggestions/clean-ups to the code would be much appreciated, as I'm still learning how to use iterators and arraylists correctly. Thanks for reading!
Code: (WIP, wanted to share it to get the errors fixed before I continued)
Main Sketch:
LaserManager LaserManage;
ShipManager ShipManage;
void setup() {
size(500,500);
LaserManage = new LaserManager();
ShipManage = new ShipManager(width/2,0,width,height);
ShipManage.Start();
}
void draw() {
background(0);
LaserManage.Manage();
ShipManage.Manage();
}
Ship class:
class Ship {
PVector Ploc,Pvel,Pacc,NewLoc; //Defines the vectors for movement, location, velocity, acceleration, and (For AI) the pathfinder location.
String Name; //Name of the ship
boolean IsAI; //IsAI: Whether the ship is run by an AI and will pathfind, or will use controls.
float Health,Mass,Size,Inertia,Team,MaxSpeedFront,AccFront,MaxSpeedSide,AccSide,MaxSpeedBack,AccBack; //Health: Ammount of health the ship has. MaxSpeedFront-AccBack: Defines the maximum speeds/accelerations of the ship.
int LaserCharge,LaserCount,MissileCharge,MissileCount; //Charnging times of the various weapons.
PImage Image; //Image used for the ship.
PVector Mouse; //These are various vectors constantly defined and used in some calculations.
Ship(String n, float x, float y, int team, int s, int m, boolean ai, PImage i, int h, float in, float sf, float ss, float sb, float af, float as, float ab) {
//Name, x starting pos, y starting pos, team, size (Per 16 pixels), mass (Per 1000kg), AI, image used, starting health.
Hi, I'm a new programmer, and I had a question about some errors I had with this sketch. I've just been experimenting with arrays and class files over the last few days, and am new to some of the syntax. In this program, I'm trying to get a series of rectangles acting as buttons to switch colors when they're pressed using arrays, just to use as a template for some more advanced code. It's not done yet, as I tested it out to see if it would work, got an Expecting TRIPLE_DOT error, and decided that I would fix this bug as well as get some feedback on how to make it better/fix other things before moving on. I looked up the TRIPLE_DOT error, and also tried to find syntax for declaring a single value for an int array inside a constructor online, but found nothing that was helpful/noob friendly.
Anyways, here's my code for the main file:
int i;
int j;
int numButtonX;
int numButtonY;
Button [][] newButton;
boolean [][] on;
void setup() {
size(500,500);
numButtonX = width/50;
numButtonY = height/50;
newButton = new Button[numButtonX][numButtonY];
on = new boolean[numButtonX][numButtonY];
for(i=0;i<numButtonX;i++) {
for(j=0;j<numButtonY;j++) {
newButton[i][j] = new Button(i,j,i*50,j*50);
on[i][j] = false;
}
}
}
void draw() {
for(i=0;i<numButtonX;i++) {
for(j=0;j<numButtonY;j++) {
newButton.display();
}
}
}
void mousePressed() {
for(i=0;i<numButtonX;i++) {
for(j=0;j<numButtonY;j++) {
on[i][j] = false;
}
}
on[round(mouseX/50)][round(mouseY/50)] = true;
}
And for the Button class:
class Button {
int [] id = new int [2];
int x;
int y;
int w = 45;
int h = 45;
Button(int tempId[0], int tempId[1], int tempX, int tempY) {