Results 1 to 10 of 10
  1. #1
    joelharkes is offline Member
    Join Date
    Jul 2010
    Location
    nethalends
    Posts
    4
    Rep Power
    0

    Default (jframe) get a variable out of array list

    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.

    Java 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;
    			}
    		}
    thanks,

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    of the other object
    If the other object is referred to by qt then
    p2.y = qt.y; // Set p2's y from qt's y

    it all didnt work.
    Please explain how or what?

  3. #3
    joelharkes is offline Member
    Join Date
    Jul 2010
    Location
    nethalends
    Posts
    4
    Rep Power
    0

    Default

    i tried to call up the array wich caused the intersect and i want that y in the p2.y

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Your explanation doesn't tell me what the code is.
    Can you show the code.

    Your posted code doesn't show an array

  5. #5
    joelharkes is offline Member
    Join Date
    Jul 2010
    Location
    nethalends
    Posts
    4
    Rep Power
    0

    Default

    code of main class:
    Java 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();
    	}	
    	
    }
    the other class:
    Java 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);
    	}
    		
    }

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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.
    Last edited by Norm; 07-08-2010 at 04:37 PM.

  7. #7
    joelharkes is offline Member
    Join Date
    Jul 2010
    Location
    nethalends
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by joelharkes View Post
    code of main class:
    Java Code:
    //name
    //Shooter Game
    //Main Class
    	
    		}
    				//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;    <<<<<-------------
    			}
    		}
    im just trying to call up the y coordinate of the gt wich is checkcig so
    something like:
    gt.x.y
    gt(x).y
    something like that but that doesnt work

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    but that doesnt work
    What variable is being set by the code shown?
    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.

  9. #9
    Moon_E is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    try this
    Java Code:
    p2.y = gt.get(x).y;

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    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

Similar Threads

  1. Jframe pass variable to Applet
    By wayn in forum AWT / Swing
    Replies: 0
    Last Post: 03-10-2010, 09:54 AM
  2. Array List help
    By Weejee37 in forum New To Java
    Replies: 4
    Last Post: 10-27-2009, 12:32 AM
  3. Array of objects, as an Instance variable
    By blaklite in forum New To Java
    Replies: 2
    Last Post: 03-25-2009, 12:56 AM
  4. adding a variable in order to a list
    By Jrr in forum New To Java
    Replies: 2
    Last Post: 11-19-2007, 01:10 AM
  5. using an Array list
    By toad in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •