Modify methods so they are given an array index value of the moon whose name or radius is required - processing

So the part of my question is ''Modify getMoonName() and getMoonRadius() so they are given an array index value of the moon whose name or radius is required.''
I've tried adding moons[i].getRadius but then end up getting ''The variable i does not exist''. Here's the code.
PLANET CLASS
public class Planet
{
private float angle=0.01;
// add class member variables here
private String name;
private float radius;
private float distance;
private float speed;
private Moon[] moons;
// add constructor here
public Planet(String n, float r, float d, float s, Moon[] m)
{
this.name=n;
this.radius=r;
this.distance=d;
this.speed=s;
this.moons=m;
}
// add other methods here
public String getName()
{
return name;
}
public float getRadius()
{
return radius;
}
public float getDistance()
{
return distance;
}
public float getSpeed()
{
return speed;
}
public Moon[] getMoons()
{
return moons;
}
public void setRadius(float r)
{
this.radius=r;
}
public String getMoonName()
{
return moons[i].getName();
}
public float getMoonRadius()
{
return moons[i].getRadius();
}
public String toString()
{
int n=0;
for (int i=0; i<moons.length; i++)
{
n++;
}
return "Planet" + name + ("Radius: " +radius +"Distance: " +distance) +n +"moons.";
}
public void printMoons()
{
for (int i=0; i<moons.length; i++)
{
System.out.println(moons[i]);
}
}
// This will display the moon when other code is completed. You don't need to understand this code.
public void display()
{
angle=angle+(0.01*speed);
pushMatrix();
rotate(angle);
translate(distance,0);
fill(255, 255, 255);
ellipse(0, 0, radius*2, radius*2);
for(Moon moon: getMoons())
moon.display();
popMatrix();
}
}`
MOON CLASS
public class Moon
{
private float angle=0.01;
// add class member variables here
private String name;
private float radius;
private float distance;
private float speed;
private int orbitalPeriod;
// add constructor here
public Moon(String n, float r, float d, float s, int o)
{
this.name=n;
this.radius=r;
this.distance=d;
this.speed=s;
this.orbitalPeriod=o;
}
// add other methods here
public String getName()
{
return name;
}
public float getRadius()
{
return radius;
}
public float getDistance()
{
return distance;
}
public float getSpeed()
{
return speed;
}
public float getOrbitalPeriod()
{
return orbitalPeriod;
}
public void setName(String n)
{
this.name=n;
}
public void setOrbitalPeriod(int o)
{
this.orbitalPeriod=o;
}
public String toString()
{
return ("Moon : " +name +" "+"orbit="+orbitalPeriod);
}
// This will display the moon when other code is completed. You don't need to understand this code.
public void display()
{
angle=angle+(0.01*speed);
pushMatrix();
rotate(angle);
translate(distance, 0);
fill(149, 149, 149);
ellipse(0, 0, radius*2, radius*2);
popMatrix();
}
}

Let's look at this function:
public String getMoonName()
{
return moons[i].getName();
}
Where do you think the i variable is defined? Your instructions say to take an argument, but this function does not take any arguments.
As a small example, let's say I had this function:
public void printMessage(){
println("Hello!");
}
If I wanted to modify that function to take a parameter, I would have to add that to the method like this:
public void printMessage(String message){
println(message);
}
You have to do something similar with your getMoonName() function.
If you're still stuck, please post a small example like mine instead of your whole sketch, and we'll go from there.

Related

how create a simple object animation in processing

QUESTION
I'm trying to create a simple object "car" animation in Processing.
My code is:
Car car;
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
car = new Car(10,10);
}
void draw(){
//refresh background
background(#818B95);
print("-");
car.drawCar();
}
void mouseClicked() {
car.run();
}
public class Car implements Runnable{
private int pos_x, pos_y;
public Car(int pos_x, int pos_y){
this.pos_x = pos_x;
this.pos_y = pos_y;
}
public void drawCar(){
rect(pos_x,pos_y,10,10);
}
public void run(){
while(true){
pos_x += 10;
print("*");
delay(200);
}
}
}
I'm expecting to see the car/rectangle move right when I click the mouse button, but nothing happens.
I've added the two print in order to see if the draw method and my car.run are executed in parallel showing some * and - printed alternately.
What I see is a sequence of - until I click and then only * are printed.
Is it possible that starting a new object thread will stop the main draw cycle?
SOLUTION
This is just a variant of the suggested solution (by Mady Daby) without using threads.
Car car;
void setup(){
size(800, 600);
background(#818B95);
frameRate(30);
car = new Car(10,10);
}
void draw(){
//refresh background
background(#818B95);
print("-");
car.drawCar();
}
void mouseClicked() {
car.moving = true;
}
public class Car{
private int pos_x, pos_y;
boolean moving = false;
public Car(int pos_x, int pos_y){
this.pos_x = pos_x;
this.pos_y = pos_y;
}
public void drawCar(){
rect(pos_x,pos_y,10,10);
//animation
if(moving){
pos_x += 10;
print("*");
}
}
}
You could make the car move right by just introducing a boolean variable to track whether the car is supposed to be moving right moving and then increment pos_x if moving is true. You can also use the clicks to toggle between moving and not moving.
Car car;
void setup() {
size(800, 600);
background(#818B95);
frameRate(30);
car = new Car(10, 10);
}
void draw() {
//refresh background
background(#818B95);
print("-");
car.drawCar();
}
void mouseClicked() {
car.toggleMoving();
}
public class Car implements Runnable {
private int pos_x, pos_y;
private boolean moving = false;
public Car(int pos_x, int pos_y) {
this.pos_x = pos_x;
this.pos_y = pos_y;
}
public void toggleMoving() {
moving = !moving;
}
public void drawCar() {
if (moving) {
this.run();
}
rect(pos_x, pos_y, 10, 10);
}
public void run() {
pos_x += 10;
print("*");
delay(200);
}
}

Trying to pick up a battery. "NullReferenceException" error

Full error is "NullReferenceException: Object reference not set to an instance of an object
battery.OnTriggerStay (UnityEngine.Collider other) (at Assets/battery.cs:32)"
heres my screen in case its something i didnt do in the inspector: http://imgur.com/a/wWSGJ
here is my code (not sure if you need my flashlight code too):
public class battery : MonoBehaviour {
public float a;
public float b;
public float c;
public float d;
public bool showText;
public int Bat;
public GameObject Flight;
public int mainBat;
public bool safeRemove;
void Start()
{
showText = false;
}
void OnTriggerStay(Collider other)
{
showText = true;
if (!safeRemove)
{
if (Input.GetKeyUp (KeyCode.E))
{
mainBat = Flight.GetComponent<flashlight> ().batLevel;
Bat = 20;
Flight.GetComponent<flashlight> ().batLevel = Bat +- mainBat;
safeRemove = true;
if (safeRemove)
{
Destroy (this.gameObject);
}
}
}
}
void OnTriggerExit(Collider other)
{
showText = false;
}
void OnGUI()
{
if (showText)
{
GUI.Box(new Rect(Screen.width/ 2.66f, Screen.height/ 3.48f, Screen.width/ 3.78f, Screen.height/ 16.1f), "Press 'E' to pick up");
}
}
}

Image Does Not Animate and is Stuck in one Frame

I'm having a problem in my code where the animation from another class does not animate and is stuck in one frame. The image moves across the screen in the right manner though. I know that the problem is somehow related to the UPDATE method of the animation. I have tried every possible solutions in my knowledge to find what causes the error and the solutions I found online were not quite helpful. Any help will be appreciated.
Here's my Code:
LevelOneScreen.java
public class LevelOneScreen implements Screen {
private final MyGame app;
WalkAnimate walkAnimate;
private Stage stage;
private Image levelOneImage;
private Image holdStartImage;
public Image walkRightImage;
public Image walkLeftImage;
public float deltaTime = Gdx.graphics.getDeltaTime();
public LevelOneScreen(final ThumbChase app){
this.app = app;
this.stage = new Stage(new StretchViewport(app.screenWidth,app.screenHeight , app.camera));
}
#Override
public void show() {
Gdx.input.setInputProcessor(stage);
walkAnimate = new WalkAnimate();
walkAnimate.update(deltaTime);
levelOneBackground();
holdStart();
ninjaWalk();
}
public void holdStart(){
Texture holdStartTexture = new Texture("HoldStart.png");
holdStartImage = new Image(holdStartTexture);
float holdStartImageW = holdStartImage.getWidth();
float holdStartImageH = holdStartImage.getHeight();
float holdStartImgWidth = app.screenWidth*0.8f;
float holdStartImgHeight = holdStartImgWidth *(holdStartImageH/holdStartImageW);
holdStartImage.isTouchable();
holdStartImage.setSize(holdStartImgWidth,holdStartImgHeight);
holdStartImage.setPosition(stage.getWidth()/2-holdStartImgWidth/2,stage.getHeight()/2-holdStartImgHeight/2);
stage.addActor(holdStartImage);
holdStartImage.addListener(new ActorGestureListener(){
/* public void touchDown (InputEvent event, float x, float y, int pointer, int button){
holdStartImage.setVisible(false);
};*/
public void fling(InputEvent event, float velocityX, float velocityY, int button) {
holdStartImage.setVisible(false);
}
public void touchDown (InputEvent event, float x, float y, int pointer, int button){
holdStartImage.setVisible(false);
};
public void touchDrag (InputEvent event, float x, float y, int pointer, int button){
holdStartImage.setVisible(false);
};
});
}
public void levelOneBackground(){
Texture levelOneTexture = new Texture("BGBlue Resize.png");
levelOneImage = new Image(levelOneTexture);
levelOneImage.setSize(app.screenWidth,app.screenHeight);
levelOneImage.setPosition(0,0);
stage.addActor(levelOneImage);
/*levelOneImage.addListener(new ActorGestureListener(){
public void touchDown (InputEvent event, float x, float y, int pointer, int button){
holdStartImage.setVisible(false);
};
});*/
}
public void ninjaWalk(){
TextureRegion ninjaWalkRight = new TextureRegion(walkAnimate.getCurrentFrameRight());
TextureRegion ninjaWalkLeft = new TextureRegion(walkAnimate.getCurrentFrameLeft());
//Texture ninjaWalkRight = new Texture("badlogic.jpg");
//Texture ninjaWalkLeft = new Texture("badlogic.jpg");
walkRightImage = new Image(ninjaWalkRight);
walkLeftImage = new Image(ninjaWalkLeft);
float walkImageW = walkRightImage.getWidth();
float walkImageH = walkRightImage.getHeight();
float walkImageWidth = app.screenWidth*0.25f;
float walkImageHeight = walkImageWidth*(walkImageH/walkImageW);
walkRightImage.isTouchable();
walkLeftImage.isTouchable();
walkRightImage.setSize(walkImageWidth,walkImageHeight);
walkLeftImage.setSize(walkImageWidth,walkImageHeight);
walkRightImage.setPosition(stage.getWidth()/2-walkImageWidth/2,0);
walkLeftImage.setPosition(stage.getWidth()/2-walkImageWidth/2,0);
walkRightImage.addAction(moveBy(app.screenWidth*0.2f,0,1f));
stage.addActor(walkRightImage);
walkRightImage.addListener(new ActorGestureListener(){
public void pan(InputEvent event, float x, float y, float deltaX, float deltaY) {
holdStartImage.setVisible(true);
}
});
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//walkAnimate.update(deltaTime);
update(delta);
}
public void update(float deltaTime){
stage.act(deltaTime);
stage.draw();
app.batch.begin();
app.batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
stage.dispose();
}
}
WalkAnimate.java
public class WalkAnimate {
public MyGame app;
public Stage stage;
private Animation walkAnimationRight;
private Animation walkAnimationLeft;
private Texture walkSheetRight;
private Texture walkSheetLeft;
private TextureRegion[] walkFramesRight;
private TextureRegion[] walkFramesLeft;
private TextureRegion currentFrameRight;
private TextureRegion currentFrameLeft;
private float stateTime;
private Rectangle bound; //used for positioning and collision detection
private static final int FRAME_COLS_WALK = 3;
private static final int FRAME_ROWS_WALK= 2;
private float screenWidth = Gdx.graphics.getWidth();
private float screenHeight = Gdx.graphics.getHeight();
public float currentFrameWidth = (float)(screenHeight*0.15);
public float currentFrameHeight = (float)(screenHeight*0.15);
public float walkSheetWidth;
public float walkSheetHeight;
public WalkAnimate () {
walkSheetRight = new Texture("ninjaWalkRight.png");
walkSheetWidth = walkSheetRight.getWidth();
walkSheetHeight = walkSheetRight.getWidth();
TextureRegion[][] tmp = TextureRegion.split(walkSheetRight, (int) walkSheetRight.getWidth() / FRAME_COLS_WALK, (int) walkSheetRight.getHeight() / FRAME_ROWS_WALK);
walkFramesRight = new TextureRegion[FRAME_COLS_WALK * FRAME_ROWS_WALK];
int index = 0 ;
for (int i = 0; i < FRAME_ROWS_WALK; i++) {
for (int j = 0; j < FRAME_COLS_WALK; j++) {
walkFramesRight[index++] = tmp[i][j];
}
}
walkAnimationRight = new Animation(0.044f, walkFramesRight);
stateTime = 0f;
walkSheetLeft = new Texture("ninjaWalkLeft.png");
walkSheetWidth = walkSheetLeft.getWidth();
walkSheetHeight = walkSheetLeft.getWidth();
TextureRegion[][] tmp1 = TextureRegion.split(walkSheetLeft, (int) walkSheetRight.getWidth() / FRAME_COLS_WALK, (int)walkSheetLeft.getHeight() / FRAME_ROWS_WALK);
walkFramesLeft = new TextureRegion[FRAME_COLS_WALK * FRAME_ROWS_WALK];
int index1 = 0;
for (int i = 0; i < FRAME_ROWS_WALK; i++) {
for (int j = 0; j < FRAME_COLS_WALK; j++) {
walkFramesLeft[index1++] = tmp1 [i][j];
}
}
walkAnimationLeft = new Animation(0.044f, walkFramesLeft);
stateTime = 0f;
currentFrameRight = walkAnimationRight.getKeyFrame(stateTime, true);
currentFrameLeft = walkAnimationLeft.getKeyFrame(stateTime, true);
}
public Rectangle getBound(){
return bound;
}
public void update(float delta){
stateTime += delta;
}
public TextureRegion getCurrentFrameRight(){
return currentFrameRight;
}
public TextureRegion getCurrentFrameLeft(){
return currentFrameLeft;
}
}
Like the earlier comment by Eames. Just by glancing at your code, I can say that you should begin by moving
walkAnimate.update(delta);
To the render section. The show section only runs once (when the class is started). Your walkAnimate.update(delta) should be running constantly to change the frame when is time. Otherwise it would only update once.
You can also do it the way I do it.
The class where you are going to use it. The 16 represents the number of images. The 1F means it is done in one second.
public void show() {
texture = new Texture("item/coin_animation.png");
animation = new Animation(new TextureRegion(texture), 16, 1f);
}
public void render(float delta) {
animation.update(delta);
}
Animation Class
public Array<TextureRegion> frames;
private float maxFrameTime;
private float currentFrameTime;
private int frameCount;
private int frame;
private static TextureRegion textureRegion;
public Animation(TextureRegion region, int frameCount, float cycleTime){
textureRegion = region;
frames = new Array<TextureRegion>();
int frameWidth = textureRegion.getRegionWidth() / frameCount;
for (int i = 0; i < frameCount; i++){
frames.add(new TextureRegion(textureRegion, i * frameWidth, 0, frameWidth, textureRegion.getRegionHeight()));
}
this.frameCount = frameCount;
maxFrameTime = cycleTime / frameCount;
frame = 0;
}
public void update(float delta){
currentFrameTime += delta;
if (currentFrameTime > maxFrameTime){
frame++;
currentFrameTime = 0;
}
if (frame >= frameCount)
frame = 0;
}
public TextureRegion getFrame(){
return frames.get(frame);
}
public void restart(){
frame = 0;
currentFrameTime = 0;
}
I will answer my question, I've tried using a separate stage for the animation and it works. I also used inputmultiplexor to set two stages both as inputprocessors. I know that this is not the best solution and I am open for more proper solutions. This is open for editing.

The method clone() from the type Object is not visible

for the life of me I can't figure out what is wrong with this code, please help. I have three classes, GeometricObject, Octagon which extends GeometricObject and TestOctagon which is being used to test the Octagon class. When I run the TestOctagon class I get this error:
The method clone() from the type Object is not visible
Here is my code:
public abstract class GeometricObject {
private String color = "white";
private boolean filled;
protected GeometricObject() {
}
protected GeometricObject(String color, boolean filled) {
this.color = color;
this.filled = filled;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFilled() {
return filled;
}
public void setFilled(boolean filled) {
this.filled = filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
import java.lang.Comparable;
import java.lang.Cloneable;
public class Octagon extends GeometricObject implements Comparable<Octagon>, Cloneable{
double side;
public Octagon() {
}
public Octagon(double side) {
this.side = side;
}
public Octagon(double side, String color, boolean filled) {
this.side = side;
setColor(color);
setFilled(filled);
}
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
public double getArea() {
return (2+4/Math.sqrt(2))*side*side;
}
public double getPerimeter() {
return 8*side;
}
#Override
public int compareTo(Octagon o) {
if (getArea() > o.getArea())
return 1;
else if (getArea() < o.getArea())
return -1;
else
return 0;
}
#Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class TestOctagon {
public static void main(String[] args) {
// TODO Auto-generated method stub
GeometricObject oc1 = new Octagon(5);
System.out.println(oc1.getArea());
System.out.println(oc1.getPerimeter());
GeometricObject oc2 = (GeometricObject)oc1.clone();
}
}
Don,
Please note that the access specifier for method Object::clone() is protected. It is not accessible from your TestOctagon class as you are invoking this method on object of type GeometriObject (oc1) where the clone method is still protected as neither it or its super classes have overridden it. Try moving the clone method from Octagon class to GeometriObject class. Please retain the public access specifier. Refer this sample on how to do it http://www.javatpoint.com/object-cloning

LibGDX Listener in Actor in Stage doesn't work

The input listener doesn't fire in the Actor even if the Actor was already added to the stage and the input processor was already set to the stage. What could be the problem here?
Inside GameScreen (extends Screen) class:
public GameScreen(Game game) {
this.game = game;
stage = new Stage(800, 480, true);
number = new Number();
stage.addActor(number);
}
public void show() {
Gdx.input.setInputProcessor(stage);
}
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
Constructor of Number:
public Number() {
addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("touch down");
return true;
}
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("touch up");
}
public boolean mouseMoved(InputEvent event, float x, float y) {
System.out.println("mouse moved");
return true;
}
});
}

Resources