Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Whats wrong whit code?  (Read 1501 times)
0 Members and 1 Guest are viewing this topic.
Offline DancingDevil

Junior Newbie





« Posted 2007-11-13 16:29:48 »

Why image doesnt move?


     package org.me.hello;

     import java.applet.*;
     import java.awt.*;
     import java.awt.event.*;
         
     public class peli extends Applet implements KeyListener {
     
     Image karkki;
     Image tikkari;
     int x,y;
     public boolean oikee;
     public boolean vasen;
     
     public void run() {
   
     while (true) {

     repaint();
         
     }   
     }
     
     public void init(){
     resize(600,400);
     x=200;
     y=350;       
     }
       
     public void keyPressed(KeyEvent e) {
     
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
             vasen = true;
          }
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
             oikee = true;
          }
     }
     
     public void keyReleased(KeyEvent e) {
     
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
              oikee = false;
          }
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
              vasen = false;
          }
     }
           
    public void keyTyped(KeyEvent e) {}
    public void actionPerformed(ActionEvent e) {}

     
    public void oikee(boolean b) {
    if(b == true){
    x = x+2;           
    }
    }

    public void vasen(boolean b) {
    if(vasen == true){
    x = x-2;
    }
    }
     
     public void paint(Graphics g) {
     tikkari = getImage(getDocumentBase(),"tikkari.png");
     karkki = getImage(getDocumentBase(),"karkki.png");
     if(oikee = true){
     x = x+2;
     }
     if(vasen = true){
     x = x+2;
     }
 
     g.drawImage(karkki,x,y,50,50,this);
   
     } 
     }

Offline fletchergames

Senior Member





« Reply #1 - Posted 2007-11-13 17:12:29 »

First, you have to add the KeyListener in the init method.  I realize that the class is the Applet and the KeyListener, but you still have to add it.  Add the line "addKeyListener(this);" to the init method.

Also, it looks like your if statements in the paint method have "=" in them.  If statements should always use "==" for boolean comparison.  I'm surprised that code even gets past the compiler.

Additionally, instead of checking the right key twice, you probably want to check the left key and the right key.

That should be enough to get the image moving, but there are some other improvements you should make.  You should only load the images in the init method and store them in variables to be used by the paint method.  In general, you shouldn't include anything except painting in the paint method.  In fact, you should remove the coordinate updating from the paint method and put it in the keyReleased method, eliminating all the code that's in the keyPressed and keyReleased methods, as well as eliminated the booleans.
Offline DancingDevil

Junior Newbie





« Reply #2 - Posted 2007-11-13 18:26:07 »

First, you have to add the KeyListener in the init method.  I realize that the class is the Applet and the KeyListener, but you still have to add it.  Add the line "addKeyListener(this);" to the init method.

Also, it looks like your if statements in the paint method have "=" in them.  If statements should always use "==" for boolean comparison.  I'm surprised that code even gets past the compiler.

Additionally, instead of checking the right key twice, you probably want to check the left key and the right key.

That should be enough to get the image moving, but there are some other improvements you should make.  You should only load the images in the init method and store them in variables to be used by the paint method.  In general, you shouldn't include anything except painting in the paint method.  In fact, you should remove the coordinate updating from the paint method and put it in the keyReleased method, eliminating all the code that's in the keyPressed and keyReleased methods, as well as eliminated the booleans.

doesnt work..
new code:

     package org.me.hello;

     import java.applet.*;
     import java.awt.*;
     import java.awt.event.*;
         
     public class peli extends Applet implements KeyListener {
     
     Image ukko;   
     Image karkki;
     Image tikkari;
     int x,y;
     public boolean oikee;
     public boolean vasen;
     
     public void run() {
   
     while (true) {

     repaint();
         
     }   
     }
     
     public void init(){
     addKeyListener(this);
     tikkari = getImage(getDocumentBase(),"tikkari.png");
     karkki = getImage(getDocumentBase(),"karkki.png");
     resize(600,400);
     x=300;
     y=350;
     }
       
     public void keyPressed(KeyEvent e) {
     
          if (e.getKeyCode() == KeyEvent.VK_LEFT) {
             vasen = true;
          }
         
         
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
             oikee = true;
          }
         
     if(oikee == true){
     x = x+2;
     }
             
         
      if(vasen == true){
     x = x+2;
     }   
         
     Image tausta;
     }
     
     public void keyReleased(KeyEvent e) {
     
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
              oikee = false;
          }
         
          if (e.getKeyCode() == KeyEvent.VK_LEFT) {
              vasen = false;
          }
     }
           
    public void keyTyped(KeyEvent e) {}
    public void actionPerformed(ActionEvent e) {}
     
     public void paint(Graphics g) {
 
     g.drawImage(karkki,x,y,50,50,this);
   
     } 
     }

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Majick

Senior Newbie





« Reply #3 - Posted 2007-11-13 19:59:21 »

Hello,
     there are demo applets from sun here: http://java.sun.com/applets/jdk/1.4/index.html take a look at theese they should help.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2007-11-13 20:08:22 »

Your big problem is that you are never calling repaint(). It's likely your applet is working, but not getting redrawn so you don't even know it. You're going to want to add repaint() to your keyPressed() method.

Speaking of that method... You don't need to use your boolean way of doing things at all. As long as a key is being held, it will continue to call the keyPressed method. So...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public void keyPressed(KeyEvent e) {
     
          if (e.getKeyCode() == KeyEvent.VK_LEFT) {
             x -= 2;
          }
         
         
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
             x += 2;
          }
          repaint();
     }


Go with that, and just get rid of everything in your keyReleased() method, and get rid of those two booleans you have. I also don't understand why you would declare an Image there... if you declare a variable and don't ever reference it in that scope, it will simply get cleaned up by the garbage collector. For that reason, I removed " Image tausta;" from the end of KeyPressed().

Also, in the future put all your code in [ code] tags when you are posting on the forums. It makes things easier for us.

See my work:
OTC Software
Offline SimonH
« Reply #5 - Posted 2007-11-13 21:08:58 »

It could use a sleep(50); in the run() method also...

People make games and games make people
Offline DancingDevil

Junior Newbie





« Reply #6 - Posted 2007-11-16 16:24:27 »

now i have an new problem:
image(donut) isnt walling normally... how do i fix this problem?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
     package org.me.hello;

     import java.applet.*;
     import java.awt.*;
     import java.awt.event.*;
         
     public class peli extends Applet implements KeyListener,Runnable {
             
     Font fontti = new Font("Helvetica", Font.BOLD,  20);    
     
     Image homer;
     Image homerv;      
     Image sydan;
     Image donut;
     
     int äks;
     int x,y;
     int pisteet,livet;
   
     public boolean oikee;
     public boolean vasen;
     public boolean tippuu;
     
     public void run(){            
                     
     while (true) {
                         
     repaint();
         
     }    
     }
     
     public void init(){
     this.äks=(int)(4*Math.random()*100+4);      
     addKeyListener(this);
     setBackground(Color.BLACK);
     donut = getImage(getDocumentBase(),"donutti.png");
     homer = getImage(getDocumentBase(),"homer.png");
     homerv = getImage(getDocumentBase(),"homerv.png");
     sydan = getImage(getDocumentBase(),"sydan.png");    
     resize(600,400);
     livet = 4;
     x=175;
     y=-70;
     }
             
     public void keyPressed(KeyEvent e) {
   
          if (e.getKeyCode() == KeyEvent.VK_ENTER) {
             tippuu = true;            
          }
         
          if (e.getKeyCode() == KeyEvent.VK_LEFT) {
             vasen = true;
          }
         
         
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
             oikee = true;
          }
         
     if(oikee == true){
     if(x<350)x = x+10;
     }
             
         
     if(vasen == true){
     if(x>0)x = x-10;
     }
                   
     repaint();
     }
     
     public void keyReleased(KeyEvent e) {
     
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
              oikee = false;
          }
         
          if (e.getKeyCode() == KeyEvent.VK_LEFT) {
              vasen = false;
          }
     }
           
    public void actionPerformed(ActionEvent e) {}
   
     public void alas(){
               
     }
     
     public void paint(Graphics g) {
     
     g.setColor(Color.red);
     g.drawRect(450,1,5,1000);
     g.setFont(fontti);
     g.drawString("Lives:",470,50);
     
     if(livet > 1){
     g.drawImage(sydan,470,65,30,30,this);
     }
     
     if(livet > 2){
     g.drawImage(sydan,500,65,30,30,this);
     }
     
     if(livet > 3){
     g.drawImage(sydan,530,65,30,30,this);
     }
     
     g.drawString("Points:"+pisteet+"",470,130);
         
     if(vasen==false){    
     g.drawImage(homer,x,313,83,100,this);
     }
   
     if(vasen==true){
     g.drawImage(homerv,x,313,83,100,this);              
     }
     
     g.drawImage(donut,äks,y,50,50,this);
     if(tippuu==true);
     }
     
    public void keyTyped(KeyEvent e) {
    }
     }

Offline fletchergames

Senior Member





« Reply #7 - Posted 2007-11-19 04:07:34 »

It's your statements like
1  
if(x>0)x = x-10;
.

If x == 5, then you'll subtract 10 and get -5.

Instead, you want to do something like:

1  
2  
3  
x -= 10;
if(x < 0)
   x = 0;


And somethign similar for hitting the other wall.  Or, at least, I think that's what you're asking about.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (46 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (36 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (51 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!