i want p2.y to be the y of the other object how do i to that? if tried much but it all didnt work.
thanks,Code:for(int x=0;x<gt.size();x++){
gt.get(x).place();
if(gt.get(x).rect.intersects(p2.rect)&&p2.vspeed>0){
p2.vspeed=0;
-----> p2.y=gt.y;
}
}
Printable View
i want p2.y to be the y of the other object how do i to that? if tried much but it all didnt work.
thanks,Code:for(int x=0;x<gt.size();x++){
gt.get(x).place();
if(gt.get(x).rect.intersects(p2.rect)&&p2.vspeed>0){
p2.vspeed=0;
-----> p2.y=gt.y;
}
}
If the other object is referred to by qt thenQuote:
of the other object
p2.y = qt.y; // Set p2's y from qt's y
Please explain how or what?Quote:
it all didnt work.
i tried to call up the array wich caused the intersect and i want that y in the p2.y
Your explanation doesn't tell me what the code is.
Can you show the code.
Your posted code doesn't show an array
code of main class:
the other class:Code://name
//Shooter Game
//Main Class
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class Shooter extends JFrame implements KeyListener{
Image img;
Graphics dbi;
boolean u,d,w, s,l,r;
int S,E;
int lengte;
Player p1= new Player(5,150,46,18,Color.GREEN,"LEFT.gif");
Player p2= new Player(300,30,46,18,Color.RED,"RIGHT.gif");
ArrayList<Bullets>b=new ArrayList<Bullets>();
ArrayList<GroundText>gt=new ArrayList<GroundText>();
public Shooter(){
setTitle("Shooter");
setSize(600,400);
setResizable(false);
setBackground(Color.BLACK);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
u=d=w=s=false;
S=E=0;
lengte=20;
setVisible(true);
}
public void paint(Graphics g) {
img=createImage(getWidth(),getHeight());
dbi=img.getGraphics();
paintComponent(dbi);
g.drawImage(img,0,0,this);
repaint();
}
public void paintComponent(Graphics g){
if(p1.health>0&&p2.health>0){
for(Bullets b1:b) {
b1.draw(g);
}
for(GroundText gt1:gt) {
gt1.draw(g);
}
update();
}
else{
if(p1.health<=0) {
g.setColor(p2.col);
g.drawString("PLAYER 2 WINS!",250,190);
}
else{
g.setColor(p1.col);
g.drawString("PLAYER 1 WINS!",250,190);
}
}
p1.draw(g);
p2.draw(g);
}
public void update(){
//sleep (20);
if(w&&p1.y>24)p1.moveUp();
if(s&&p1.y<347)p1.moveDown();
if(r&&p2.x<580)p2.moveRight();
if(l&&p2.x>20)p2.moveLeft();
if(u&&p2.y>24)p2.moveUp();
if(d&&p2.y<347)p2.moveDown();
if(E==1){
Bullets add=p2.getBull();
add.xVel=-3;
b.add(add);
E++;
}
if(S==1){
Bullets add=p1.getBull();
add.xVel=3;
b.add(add);
S++;
}
while(lengte>=0){
GroundText add=new GroundText(lengte,360,"Texture1.gif");
lengte--;
gt.add(add);
}
//watch if instersects ground
for(int x=0;x<gt.size();x++){
gt.get(x).place();
if(gt.get(x).rect.intersects(p2.rect)&&p2.vspeed>0){
p2.vspeed=0;
p2.y=gt.y;
}
}
p2.Gravity();
for(int x=0;x<b.size();x++){
b.get(x).move();
if(b.get(x).rect.intersects(p1.rect)&&b.get(x).xVel<0){
p1.health--;
b.remove(x);
x--;
continue;
}
if(b.get(x).rect.intersects(p2.rect)&&b.get(x).xVel>0){
p2.health--;
b.remove(x);
x--;
continue;
}
}
}
public void keyTyped(KeyEvent e){}
public void keyPressed (KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:u=true;break;
case KeyEvent.VK_DOWN:d=true;break;
case KeyEvent.VK_LEFT:l=true;break;
case KeyEvent.VK_RIGHT:r=true;break;
case KeyEvent.VK_W:w=true;break;
case KeyEvent.VK_S:s=true;break;
case KeyEvent.VK_SPACE:S++;break;
case KeyEvent.VK_ENTER:E++;break;
}
}
public void keyReleased(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_UP:u=false;
case KeyEvent.VK_DOWN:d=false;
case KeyEvent.VK_LEFT:l=false;
case KeyEvent.VK_RIGHT:r=false;
case KeyEvent.VK_W:w=false;
case KeyEvent.VK_S:s=false;
case KeyEvent.VK_SPACE:S=0;break;
case KeyEvent.VK_ENTER:E=0;break;
}
}
public static void main(String[]beans){
KeyListener s=new Shooter();
}
}
Code://name
//Shooter Game
//GroundText Class
import java.awt.*;
public class GroundText {
int x,y,lengte;
int height,width;
Image img;
Rectangle rect;
public GroundText(){
x=y=height=width=0;
rect=new Rectangle(x,y,width,height);
}
public GroundText(int lengte,int y,String s){
this.x=lengte*32;
this.y=y;
height=32;
width=32;
img=Toolkit.getDefaultToolkit().getImage(s);
rect=new Rectangle(x,y,width,height);
}
public GroundText(int x,int y,int wd,int ht){
this.x=x;
this.y=y;
height=ht;
width=wd;
rect=new Rectangle(x,y,width,height);
}
public void draw(Graphics g){
g.drawImage(img,x,y,null);
}
public void setImage(String s){
img=Toolkit.getDefaultToolkit().getImage(s);
}
public void place(){
rect.setLocation(x,y);
}
}
Ok, now can you put some comments in the code showing where the problem is?
In case you don't know what a comment is, its something like this:
/* comments here */
or
// This is another way to comment
Whoops, I missed that you have 2 comments in the code.
What variable is being set by the code shown?Quote:
but that doesnt work
p2.y=gt.y;
Where is the p2 object? Does its member y have the value from gt.y after your assignment statement?
Are there other objects of the same type as p2 that could confuse you?
Add some debugging code to show when and where the value of y is changed for ALL Player objects. The println() statement will need to include some id for the Player's who's y value is being changed.
try this
Code:p2.y = gt.get(x).y;
If that works it's interesting that gt has a member y and the object returned by get() also has a y.
Makes for confusion and errors. QED