Results 1 to 10 of 10
- 07-08-2010, 11:05 AM #1
Member
- Join Date
- Jul 2010
- Location
- nethalends
- Posts
- 4
- Rep Power
- 0
- 07-08-2010, 12:56 PM #2
If the other object is referred to by qt thenof the other object
p2.y = qt.y; // Set p2's y from qt's y
Please explain how or what?it all didnt work.
- 07-08-2010, 03:07 PM #3
Member
- Join Date
- Jul 2010
- Location
- nethalends
- Posts
- 4
- Rep Power
- 0
i tried to call up the array wich caused the intersect and i want that y in the p2.y
- 07-08-2010, 03:16 PM #4
Your explanation doesn't tell me what the code is.
Can you show the code.
Your posted code doesn't show an array
- 07-08-2010, 04:24 PM #5
Member
- Join Date
- Jul 2010
- Location
- nethalends
- Posts
- 4
- Rep Power
- 0
code of main class:
the other 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(); } }
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); } }
- 07-08-2010, 04:33 PM #6
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.
- 07-08-2010, 05:12 PM #7
Member
- Join Date
- Jul 2010
- Location
- nethalends
- Posts
- 4
- Rep Power
- 0
- 07-08-2010, 05:18 PM #8
What variable is being set by the code shown?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.
- 07-08-2010, 09:46 PM #9
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
try this
Java Code:p2.y = gt.get(x).y;
- 07-08-2010, 10:38 PM #10
Similar Threads
-
Jframe pass variable to Applet
By wayn in forum AWT / SwingReplies: 0Last Post: 03-10-2010, 09:54 AM -
Array List help
By Weejee37 in forum New To JavaReplies: 4Last Post: 10-27-2009, 12:32 AM -
Array of objects, as an Instance variable
By blaklite in forum New To JavaReplies: 2Last Post: 03-25-2009, 12:56 AM -
adding a variable in order to a list
By Jrr in forum New To JavaReplies: 2Last Post: 11-19-2007, 01:10 AM -
using an Array list
By toad in forum New To JavaReplies: 1Last Post: 11-18-2007, 09:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks