Animando Sprites

3 respostas
L

Olá.
Estou começando a animar alguns sprites e me surgiu uma dúvida.

Aqui o código:

Código principal

package tutorial8;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class Game1 extends JFrame implements KeyListener {
	
	BufferedImage buffer;
	int FPS = 30;
	char teclaPressionada;
	
	ImageIcon image = new ImageIcon("src/tutorial8/s1.gif");
	
    Sprite bixo1 = new Sprite(3,400,100);
    Sprite bixo2 = new Sprite(3,100,200);
	
    public void atualizar() {
    	
    }
    
    public void dGraficos() {
    	Graphics g = getGraphics(); 
    	Graphics bbg = buffer.getGraphics();
    	
    	bbg.setColor(Color.WHITE);
    	bbg.fillRect(0, 0, 500, 500);
    	
    	bbg.drawImage(bixo1.cenas[bixo1.cena].getImage(),400,100,this);    	
    	g.drawImage(buffer, 0, 0, this);
    	
    }
    
    public void inicializar() {
    	setTitle("SkiFi Games");
    	setSize(500,500);
    	setResizable(false);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    	setVisible(true);
    	
    	buffer = new BufferedImage(500,500, BufferedImage.TYPE_INT_RGB);
    	
    	addKeyListener(this);
    	
    	bixo1.cenas[0] = new ImageIcon("src/tutorial8/s1.gif"); 
        bixo1.cenas[1] = new ImageIcon("src/tutorial8/s2.gif");
        bixo1.cenas[2] = new ImageIcon("src/tutorial8/s3.gif");
    
    }
    
    public void run() {
    	inicializar();
    	while(true) {
    		dGraficos();
    		try {
				Thread.sleep(4000/FPS);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
    	}
    }
    
    public static void main(String[]args) {
    	Game1 game = new Game1();
    	game.run();
    }

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		teclaPressionada = e.getKeyChar();
		if(e.getKeyCode()==e.VK_W) {
			bixo1.animar();
			
		}
		if(e.getKeyCode()==e.VK_LEFT) {
			bixo1.x -= 10;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
}

Código de animação

package tutorial8;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Sprite extends JFrame {
	
	ImageIcon cenas[];
    boolean pode;
	int x;
	int y;
	int largura;
	int altura;
	int cena = 0;
	int controlaVelocidade = 0;
	int velocidade = 5;
	
	public Sprite(int nCenas, int x, int y) {
		cenas = new ImageIcon[nCenas];
		this.x = x;
		this.y = y;
	}
	
	public void animar() {
		cena++;
		if(cena == cenas.length) {
			cena = 0;
		}
	}
	
	public void SlowAnimation() {
		controlaVelocidade++;
		if(controlaVelocidade>velocidade) {
			cena++;
			controlaVelocidade = 0;
			if(cena == cenas.length) {
				cena = 0;
			}
		}
	}
}

Então, a minha dúvida é a seguinte:
Como eu poderia fazer com que quando eu aperte “W” apareçam
as 3 imagens de umas vez, sem que eu precise apertar “W” 3 vezes para animar.
Não sei se vocês me entenderam… Espero que alguem possa me ajudar
Abraço.

3 Respostas

R

curti o tópico, tb procuro isto.

ver: [url]http://www.guj.com.br/java/305510-animar-um-imageicon#1625544[/url]

com w funcionou.

segue jogo onde quero animação... os robos que surgem, quero que estejam animados...

http://www.guj.com.br/java/305477-novo-jogo--em-java-------dor-destrua-os-robos

fiz umas mudanças, que anima quando tecla a tecla esquerda...

nos respectivos lugares, as mudanças...

if(e.getKeyCode()==e.VK_LEFT) {  
            bixo1.x -= 10; 
            System.out.println(" x"+bixo1.x);
            System.out.println("cliquei esquerda...");            
             bixo1.SlowAnimation(); 
            
        }
public void SlowAnimation() {  
        controlaVelocidade++;  
        if(controlaVelocidade>velocidade) {  
            cena++;  
            controlaVelocidade = 0;  
            if(cena == cenas.length) {  
                cena = 0;  
            }  
        }  
    }

e tb:

ImageIcon cenas[];  
    boolean pode;  
    int x;  
    int y;  
    int largura;  
    int altura;  
    int cena = 0;  
    int controlaVelocidade = 0;  
    int velocidade = 0;
R

consegui uma animação com a tecla esquerda, mas ele não anda de jeito nenhum, alguém ve porque não consigo mover o sprite...??

grato...

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUJ;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Game extends JFrame implements KeyListener {

    BufferedImage buffer;
    int FPS = 30;
    char teclaPressionada;
    // ImageIcon image = new ImageIcon("src/tutorial8/s1.gif");  
    int x = 400;
    int y = 100;
    ImageIcon image = new ImageIcon(getClass().getResource("parte1_robo.png"));
    ImageIcon parte2_robo = new ImageIcon(getClass().getResource("parte2_robo.png"));
    ImageIcon parte3_robo = new ImageIcon(getClass().getResource("parte3_robo.png"));
    Sprite bixo1 = new Sprite(3, x, 100);
    Sprite bixo2 = new Sprite(3, 100, 200);
    
   // ImageIcon  bixo1.cenas[0];

    public void atualizar() {
    }

    public void dGraficos() {
        Graphics g = getGraphics();
        Graphics bbg = buffer.getGraphics();
        bbg.setColor(Color.WHITE);
        bbg.fillRect(0, 0, 500, 500);
      
    
        bbg.drawImage(bixo1.cenas[bixo1.cena].getImage(), x, y, this);
        g.drawImage(buffer, 0, 0, this);
   
    }

    public void inicializar() {
        setTitle("SkiFi Games");
        setSize(500, 500);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        buffer = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
        addKeyListener(this);
        bixo1.cenas[0] = new ImageIcon(getClass().getResource("parte1_robo.png"));
        bixo1.cenas[1] = new ImageIcon(getClass().getResource("parte2_robo.png"));
        bixo1.cenas[2] = new ImageIcon(getClass().getResource("parte3_robo.png"));
    }
    public void run2() {
        inicializar();
        while (true) {  
            dGraficos();
            try {
                Thread.sleep(4000 / FPS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(" bixo1.x" + bixo1.x);
             
        }
    }

    public static void main(String[] args) {
        Game game = new Game();
        game.run2();
    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub  
        teclaPressionada = e.getKeyChar();
        if (e.getKeyCode() == e.VK_W) {
            bixo1.animar();
            bixo1.y = bixo1.y + 10;
        }
        if (e.getKeyCode() == e.VK_LEFT) {
            bixo1.x -= 10;
            System.out.println(" x" + bixo1.x);
            System.out.println("cliquei esquerda...");
            bixo1.SlowAnimation();
        
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub  
    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub  
    }
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUJ;

import java.awt.Graphics;  
import java.awt.image.BufferedImage;  
  
import javax.swing.ImageIcon;  
import javax.swing.JFrame;  
  
public class Sprite extends JFrame {  
      
    ImageIcon cenas[];  
    boolean pode;  
    int x;  
    int y;  
    int largura;  
    int altura;  
    int cena = 0;  
    int controlaVelocidade = 0;  
    int velocidade = 0;  
      
    public Sprite(int nCenas, int x, int y) {  
       cenas = new ImageIcon[nCenas];  
        this.x = x;  
        this.y = y;  
    }  
     
    
    public void Sprite2( int x, int y) {  
     //   cenas = new ImageIcon[nCenas];  
        
        this.x=x;
        this.y=y;
    }  
     
    public void animar() {  
        cena++;  
        if(cena == cenas.length) {  
            cena = 0;  
        }  
    }  
      
    public void SlowAnimation() {  
        controlaVelocidade++;  
        if(controlaVelocidade>velocidade) {  
            cena++;  
            controlaVelocidade = 0;  
            if(cena == cenas.length) {  
                cena = 0;  
            }  
        }  
    }  
}
R

o x muda mas não atualiza a imagem…

Criado 2 de maio de 2013
Ultima resposta 22 de out. de 2013
Respostas 3
Participantes 2