How to create multiple, identical objects that chase a single object [Java]
I have been having some trouble with attempting to create constantly "spawning" objects (called Rushers) that chase after a single object that is user-controlled (called Character). My main issue is the coordinates for the Rushers seem to be the exact same as that of the Character.
I have my code split into several classes. Here is the code for the Character first:
public class Gamepanel {
private boolean right = false, left = false, up = false, down = false;
public Character mainChar;
private static int xCoor = 230;
private static int yCoor = 210;
This is the main loop:
public void tick(){
mainChar = new Character(xCoor, yCoor, 30, 50);
mainChar.setxCoor(xCoor);
mainChar.setyCoor(yCoor);
if(right && mainChar.getxCoor() < 469) xCoor+=5;
if(left && mainChar.getxCoor() > 0) xCoor -= 5;
if(up && mainChar.getyCoor() > 0) yCoor -= 5;
if(down && mainChar.getyCoor() < 449) yCoor+=5;
}
The GFX:
mainChar.draw(g);
The controls are basic keyPressed and keyReleased methods. I will not post them for sake of brevity, as they seem to be working fine.
This is the code for the Character class:
public class Character {
int xCoor = 230;
int yCoor = 210;
int width = 30;
int height = 50;
public Character(int xCoor, int yCoor, int width, int height) {
this.xCoor = xCoor;
this.yCoor = yCoor;
}
public void draw(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect(xCoor, yCoor, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
this.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
this.yCoor = yCoor;
}
}
These all appear to work as normal with no massive issues. The biggest problems I was having was with my other objects, the Rushers. Here is their code in the Gamepanel:
public class Gamepanel {
private Rusher rusher;
ArrayList<Rusher> rushers;
int spawnTimer = 0;
public Gamepanel() {
rushers = new ArrayList <Rusher>();
}
public void tick(){
spawnTimer++;
if(spawnTimer > 75) {
spawn();
spawnTimer = 0;
}
if(rushers.size() > 0) {
for(int i = 0; i < rushers.size(); i++) {
rushers.get(i).tick();
}
}
}
The GFX:
if(rushers.size() > 0) {
for(int i = 0 ; i < rushers.size(); i++) {
rushers.get(i).draw(g);
}
}
Spawn method (pretty sure the error is in here):
public void spawn() {
int xSpawn = new int[4];
int ySpawn = new int[4];
ySpawn[0] = 250; ySpawn[1] = 499; ySpawn[2] = 250; ySpawn[3] = 0;
xSpawn[0] = 0; xSpawn[1] = 250; xSpawn[2] = 499; xSpawn[3] = 250;
int spawnCoor = randomRange(0, 3);
rusher = new Rusher(xSpawn[spawnCoor], ySpawn[spawnCoor], 10, 10);
rusher.setxCoor(xSpawn[spawnCoor]);
rusher.setyCoor(ySpawn[spawnCoor]);
rushers.add(rusher);
}
and finally, the Rusher class:
public class Rusher {
private static int xCoor;
private static int yCoor;
private int width = 20;
private int height = 20;
public Rusher(int xCoor, int yCoor, int width, int height) {
super(xCoor, yCoor, width, height);
Rusher.xCoor = xCoor;
Rusher.yCoor = yCoor;
}
public void tick() {
if(xCoor > Gamepanel.mainxCoor()) xCoor -= 2;
if(yCoor > Gamepanel.mainyCoor()) yCoor -= 2;
if(xCoor < Gamepanel.mainxCoor()) xCoor += 2;
if(yCoor < Gamepanel.mainyCoor()) yCoor += 2;
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillRect(xCoor, yCoor, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
Rusher.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
Rusher.yCoor = yCoor;
}
}
Just to recap, the main issue is that the Rushers will spawn with the same coordinates as the Character, and will also move just as fast, despite me setting their motion to 3 slower than the Character. I tried to make this as brief as possible. If more code is needed, I do have some others that relate to these objects.
Thanks in advance.
java oop jpanel
add a comment |
I have been having some trouble with attempting to create constantly "spawning" objects (called Rushers) that chase after a single object that is user-controlled (called Character). My main issue is the coordinates for the Rushers seem to be the exact same as that of the Character.
I have my code split into several classes. Here is the code for the Character first:
public class Gamepanel {
private boolean right = false, left = false, up = false, down = false;
public Character mainChar;
private static int xCoor = 230;
private static int yCoor = 210;
This is the main loop:
public void tick(){
mainChar = new Character(xCoor, yCoor, 30, 50);
mainChar.setxCoor(xCoor);
mainChar.setyCoor(yCoor);
if(right && mainChar.getxCoor() < 469) xCoor+=5;
if(left && mainChar.getxCoor() > 0) xCoor -= 5;
if(up && mainChar.getyCoor() > 0) yCoor -= 5;
if(down && mainChar.getyCoor() < 449) yCoor+=5;
}
The GFX:
mainChar.draw(g);
The controls are basic keyPressed and keyReleased methods. I will not post them for sake of brevity, as they seem to be working fine.
This is the code for the Character class:
public class Character {
int xCoor = 230;
int yCoor = 210;
int width = 30;
int height = 50;
public Character(int xCoor, int yCoor, int width, int height) {
this.xCoor = xCoor;
this.yCoor = yCoor;
}
public void draw(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect(xCoor, yCoor, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
this.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
this.yCoor = yCoor;
}
}
These all appear to work as normal with no massive issues. The biggest problems I was having was with my other objects, the Rushers. Here is their code in the Gamepanel:
public class Gamepanel {
private Rusher rusher;
ArrayList<Rusher> rushers;
int spawnTimer = 0;
public Gamepanel() {
rushers = new ArrayList <Rusher>();
}
public void tick(){
spawnTimer++;
if(spawnTimer > 75) {
spawn();
spawnTimer = 0;
}
if(rushers.size() > 0) {
for(int i = 0; i < rushers.size(); i++) {
rushers.get(i).tick();
}
}
}
The GFX:
if(rushers.size() > 0) {
for(int i = 0 ; i < rushers.size(); i++) {
rushers.get(i).draw(g);
}
}
Spawn method (pretty sure the error is in here):
public void spawn() {
int xSpawn = new int[4];
int ySpawn = new int[4];
ySpawn[0] = 250; ySpawn[1] = 499; ySpawn[2] = 250; ySpawn[3] = 0;
xSpawn[0] = 0; xSpawn[1] = 250; xSpawn[2] = 499; xSpawn[3] = 250;
int spawnCoor = randomRange(0, 3);
rusher = new Rusher(xSpawn[spawnCoor], ySpawn[spawnCoor], 10, 10);
rusher.setxCoor(xSpawn[spawnCoor]);
rusher.setyCoor(ySpawn[spawnCoor]);
rushers.add(rusher);
}
and finally, the Rusher class:
public class Rusher {
private static int xCoor;
private static int yCoor;
private int width = 20;
private int height = 20;
public Rusher(int xCoor, int yCoor, int width, int height) {
super(xCoor, yCoor, width, height);
Rusher.xCoor = xCoor;
Rusher.yCoor = yCoor;
}
public void tick() {
if(xCoor > Gamepanel.mainxCoor()) xCoor -= 2;
if(yCoor > Gamepanel.mainyCoor()) yCoor -= 2;
if(xCoor < Gamepanel.mainxCoor()) xCoor += 2;
if(yCoor < Gamepanel.mainyCoor()) yCoor += 2;
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillRect(xCoor, yCoor, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
Rusher.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
Rusher.yCoor = yCoor;
}
}
Just to recap, the main issue is that the Rushers will spawn with the same coordinates as the Character, and will also move just as fast, despite me setting their motion to 3 slower than the Character. I tried to make this as brief as possible. If more code is needed, I do have some others that relate to these objects.
Thanks in advance.
java oop jpanel
add a comment |
I have been having some trouble with attempting to create constantly "spawning" objects (called Rushers) that chase after a single object that is user-controlled (called Character). My main issue is the coordinates for the Rushers seem to be the exact same as that of the Character.
I have my code split into several classes. Here is the code for the Character first:
public class Gamepanel {
private boolean right = false, left = false, up = false, down = false;
public Character mainChar;
private static int xCoor = 230;
private static int yCoor = 210;
This is the main loop:
public void tick(){
mainChar = new Character(xCoor, yCoor, 30, 50);
mainChar.setxCoor(xCoor);
mainChar.setyCoor(yCoor);
if(right && mainChar.getxCoor() < 469) xCoor+=5;
if(left && mainChar.getxCoor() > 0) xCoor -= 5;
if(up && mainChar.getyCoor() > 0) yCoor -= 5;
if(down && mainChar.getyCoor() < 449) yCoor+=5;
}
The GFX:
mainChar.draw(g);
The controls are basic keyPressed and keyReleased methods. I will not post them for sake of brevity, as they seem to be working fine.
This is the code for the Character class:
public class Character {
int xCoor = 230;
int yCoor = 210;
int width = 30;
int height = 50;
public Character(int xCoor, int yCoor, int width, int height) {
this.xCoor = xCoor;
this.yCoor = yCoor;
}
public void draw(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect(xCoor, yCoor, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
this.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
this.yCoor = yCoor;
}
}
These all appear to work as normal with no massive issues. The biggest problems I was having was with my other objects, the Rushers. Here is their code in the Gamepanel:
public class Gamepanel {
private Rusher rusher;
ArrayList<Rusher> rushers;
int spawnTimer = 0;
public Gamepanel() {
rushers = new ArrayList <Rusher>();
}
public void tick(){
spawnTimer++;
if(spawnTimer > 75) {
spawn();
spawnTimer = 0;
}
if(rushers.size() > 0) {
for(int i = 0; i < rushers.size(); i++) {
rushers.get(i).tick();
}
}
}
The GFX:
if(rushers.size() > 0) {
for(int i = 0 ; i < rushers.size(); i++) {
rushers.get(i).draw(g);
}
}
Spawn method (pretty sure the error is in here):
public void spawn() {
int xSpawn = new int[4];
int ySpawn = new int[4];
ySpawn[0] = 250; ySpawn[1] = 499; ySpawn[2] = 250; ySpawn[3] = 0;
xSpawn[0] = 0; xSpawn[1] = 250; xSpawn[2] = 499; xSpawn[3] = 250;
int spawnCoor = randomRange(0, 3);
rusher = new Rusher(xSpawn[spawnCoor], ySpawn[spawnCoor], 10, 10);
rusher.setxCoor(xSpawn[spawnCoor]);
rusher.setyCoor(ySpawn[spawnCoor]);
rushers.add(rusher);
}
and finally, the Rusher class:
public class Rusher {
private static int xCoor;
private static int yCoor;
private int width = 20;
private int height = 20;
public Rusher(int xCoor, int yCoor, int width, int height) {
super(xCoor, yCoor, width, height);
Rusher.xCoor = xCoor;
Rusher.yCoor = yCoor;
}
public void tick() {
if(xCoor > Gamepanel.mainxCoor()) xCoor -= 2;
if(yCoor > Gamepanel.mainyCoor()) yCoor -= 2;
if(xCoor < Gamepanel.mainxCoor()) xCoor += 2;
if(yCoor < Gamepanel.mainyCoor()) yCoor += 2;
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillRect(xCoor, yCoor, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
Rusher.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
Rusher.yCoor = yCoor;
}
}
Just to recap, the main issue is that the Rushers will spawn with the same coordinates as the Character, and will also move just as fast, despite me setting their motion to 3 slower than the Character. I tried to make this as brief as possible. If more code is needed, I do have some others that relate to these objects.
Thanks in advance.
java oop jpanel
I have been having some trouble with attempting to create constantly "spawning" objects (called Rushers) that chase after a single object that is user-controlled (called Character). My main issue is the coordinates for the Rushers seem to be the exact same as that of the Character.
I have my code split into several classes. Here is the code for the Character first:
public class Gamepanel {
private boolean right = false, left = false, up = false, down = false;
public Character mainChar;
private static int xCoor = 230;
private static int yCoor = 210;
This is the main loop:
public void tick(){
mainChar = new Character(xCoor, yCoor, 30, 50);
mainChar.setxCoor(xCoor);
mainChar.setyCoor(yCoor);
if(right && mainChar.getxCoor() < 469) xCoor+=5;
if(left && mainChar.getxCoor() > 0) xCoor -= 5;
if(up && mainChar.getyCoor() > 0) yCoor -= 5;
if(down && mainChar.getyCoor() < 449) yCoor+=5;
}
The GFX:
mainChar.draw(g);
The controls are basic keyPressed and keyReleased methods. I will not post them for sake of brevity, as they seem to be working fine.
This is the code for the Character class:
public class Character {
int xCoor = 230;
int yCoor = 210;
int width = 30;
int height = 50;
public Character(int xCoor, int yCoor, int width, int height) {
this.xCoor = xCoor;
this.yCoor = yCoor;
}
public void draw(Graphics g) {
g.setColor(Color.GREEN);
g.fillRect(xCoor, yCoor, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
this.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
this.yCoor = yCoor;
}
}
These all appear to work as normal with no massive issues. The biggest problems I was having was with my other objects, the Rushers. Here is their code in the Gamepanel:
public class Gamepanel {
private Rusher rusher;
ArrayList<Rusher> rushers;
int spawnTimer = 0;
public Gamepanel() {
rushers = new ArrayList <Rusher>();
}
public void tick(){
spawnTimer++;
if(spawnTimer > 75) {
spawn();
spawnTimer = 0;
}
if(rushers.size() > 0) {
for(int i = 0; i < rushers.size(); i++) {
rushers.get(i).tick();
}
}
}
The GFX:
if(rushers.size() > 0) {
for(int i = 0 ; i < rushers.size(); i++) {
rushers.get(i).draw(g);
}
}
Spawn method (pretty sure the error is in here):
public void spawn() {
int xSpawn = new int[4];
int ySpawn = new int[4];
ySpawn[0] = 250; ySpawn[1] = 499; ySpawn[2] = 250; ySpawn[3] = 0;
xSpawn[0] = 0; xSpawn[1] = 250; xSpawn[2] = 499; xSpawn[3] = 250;
int spawnCoor = randomRange(0, 3);
rusher = new Rusher(xSpawn[spawnCoor], ySpawn[spawnCoor], 10, 10);
rusher.setxCoor(xSpawn[spawnCoor]);
rusher.setyCoor(ySpawn[spawnCoor]);
rushers.add(rusher);
}
and finally, the Rusher class:
public class Rusher {
private static int xCoor;
private static int yCoor;
private int width = 20;
private int height = 20;
public Rusher(int xCoor, int yCoor, int width, int height) {
super(xCoor, yCoor, width, height);
Rusher.xCoor = xCoor;
Rusher.yCoor = yCoor;
}
public void tick() {
if(xCoor > Gamepanel.mainxCoor()) xCoor -= 2;
if(yCoor > Gamepanel.mainyCoor()) yCoor -= 2;
if(xCoor < Gamepanel.mainxCoor()) xCoor += 2;
if(yCoor < Gamepanel.mainyCoor()) yCoor += 2;
}
public void draw(Graphics g) {
g.setColor(Color.RED);
g.fillRect(xCoor, yCoor, width, height);
}
public int getxCoor() {
return xCoor;
}
public void setxCoor(int xCoor) {
Rusher.xCoor = xCoor;
}
public int getyCoor() {
return yCoor;
}
public void setyCoor(int yCoor) {
Rusher.yCoor = yCoor;
}
}
Just to recap, the main issue is that the Rushers will spawn with the same coordinates as the Character, and will also move just as fast, despite me setting their motion to 3 slower than the Character. I tried to make this as brief as possible. If more code is needed, I do have some others that relate to these objects.
Thanks in advance.
java oop jpanel
java oop jpanel
asked Nov 25 '18 at 1:28
jlars789jlars789
286
286
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I found it out. I was setting the coordinates for the Rushers as static, so they all shared the same ones. When I changed that, and adjusted some other, smaller things, it worked out much better.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463924%2fhow-to-create-multiple-identical-objects-that-chase-a-single-object-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I found it out. I was setting the coordinates for the Rushers as static, so they all shared the same ones. When I changed that, and adjusted some other, smaller things, it worked out much better.
add a comment |
I found it out. I was setting the coordinates for the Rushers as static, so they all shared the same ones. When I changed that, and adjusted some other, smaller things, it worked out much better.
add a comment |
I found it out. I was setting the coordinates for the Rushers as static, so they all shared the same ones. When I changed that, and adjusted some other, smaller things, it worked out much better.
I found it out. I was setting the coordinates for the Rushers as static, so they all shared the same ones. When I changed that, and adjusted some other, smaller things, it worked out much better.
answered Nov 25 '18 at 15:43
jlars789jlars789
286
286
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463924%2fhow-to-create-multiple-identical-objects-that-chase-a-single-object-java%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown