Author Topic: Making sprite move across screen  (Read 1149 times)

0 Members and 1 Guest are viewing this topic.

Offline m1kesanders

  • Serf
  • *
  • Posts: 48
  • Cookies: -5
  • I'm sarcastic, somewhat new, and a grey hat
    • View Profile
Making sprite move across screen
« on: March 23, 2014, 09:58:59 pm »
Alright so I'm back, I figured out the keyEvent thanks everyone who helped :), but now my problem is getting the spell to fire and continuously move across the screen until it hits the right border, as it is I have it so it moves, but only increments one every time I press space I want it to automatically increment after just one press, well here's my code any help is greatly appreciated thanks

I think those are the only classes you'll need, thanks again I looked everywhere on Google and couldn't find a thing anyway thanks :) 

KeyHandeler
Code: (javascript) [Select]
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ImageIcon;


@SuppressWarnings("serial")
public class WizardKeyHandeler extends WizardBattleGrid implements KeyListener{


@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();

if(key == KeyEvent.VK_W){

if(getGoodGuy().getx() != 0){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()-1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}

}

else if(key == KeyEvent.VK_S){

if(getGoodGuy().getx() != 19){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()+1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_D){

if(getGoodGuy().gety() != 9){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()+1)].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_A){

if(getGoodGuy().gety() != 0){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()-1)].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_SPACE){
if(getBlueSpell().gety() != 19){
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
WizardCells[getBlueSpell().changex(getGoodGuy().getx())][getBlueSpell().changey(getBlueSpell().gety() +1)].setIcon(getBlueSpell().getIcon());

}

}

}





@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(getGoodGuy().getx() != 0){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_S){

if(getGoodGuy().getx() != 19){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_D){

if(getGoodGuy().gety() != 9){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_A){

if(getGoodGuy().gety() != 0){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_SPACE){

if(getBlueSpell().gety() != 19){

WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(getBlueSpell().getIcon());

}
else{
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
}
}

}










@Override
public void keyTyped(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(getGoodGuy().getx() != 0){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()-1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());

}

}

else if(key == KeyEvent.VK_S){

if(getGoodGuy().getx() != 19){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()+1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_D){

if(getGoodGuy().gety() != 9){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()+1)].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_A){

if(getGoodGuy().gety() != 0){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()-1)].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_SPACE){
if(getBlueSpell().gety() != 19){
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
WizardCells[getBlueSpell().changex(getGoodGuy().getx())][getBlueSpell().changey(getBlueSpell().gety()+1)].setIcon(getBlueSpell().getIcon());

}

}

}









}







BlueSpell

Code: (javascript) [Select]
import javax.swing.ImageIcon;


public class BlueSpell extends WizardBattleGrid{
private int BlueSpellx;
private int BlueSpelly;
private ImageIcon Blue;

public BlueSpell(int x, int y, ImageIcon BlueSpell){
BlueSpellx = x;
BlueSpelly = y;
Blue = BlueSpell;

}

public int getx(){

return BlueSpellx;
}

public int gety(){

return BlueSpelly;
}

public ImageIcon getIcon(){

return Blue;
}

public int changex(int x){

BlueSpellx = x;

return x;
}

public int changey(int y){

BlueSpelly = y;

return y;
}





}


Grid
Code: (javascript) [Select]
import java.awt.*;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;


@SuppressWarnings("serial")

public class WizardBattleGrid extends GameWindowWizard {
public static JLabel[][] WizardCells = new JLabel [20][20];
public static int pos1 = 0;
public static int pos2 = 0;
public static JPanel WizardPanel = new JPanel(new GridLayout(20, 20, 0, 0));










public static void CreateGrid(){


for(int i=0; i<WizardCells.length; i++ ){
for(int j=0; j<WizardCells[i].length; j++){

JLabel label = new JLabel();
WizardCells[i][j] = label;

}
}

for(int i=0; i<WizardCells.length; i++){
for(int j=0; j<WizardCells[i].length;j++){


WizardPanel.add(WizardCells[i][j]);


}

}

WizardCells[pos1+10][pos2].setIcon(GoodGuyWizardIcon());
WizardCells[pos1+10][pos2+19].setIcon(BadGuyWizard());





Window.add(WizardPanel);

}




}




Window
Code: (javascript) [Select]
import java.awt.BorderLayout;

import java.awt.Image;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;




@SuppressWarnings("serial")
public class GameWindowWizard extends JFrame {

final static JFrame Window = new JFrame();
static JButton Start = new JButton("Start");
static JButton Exit = new JButton("Exit");
static JPanel MainMenuButtons = new JPanel();
private static JLabel WizardBackground = new JLabel();
public static JLabel GoodGuyWizardLabel = new JLabel();
private static GoodGuy GoodGuyWizard = new GoodGuy(10, 0, GoodGuyWizardIcon());
private static BlueSpell GoodSpell = new BlueSpell(getGoodGuy().getx(), getGoodGuy().gety(), BlueSpellWizard());

public static GoodGuy getGoodGuy(){
return GoodGuyWizard;
}


public static BlueSpell getBlueSpell(){
return GoodSpell;
}

public static ImageIcon BlueSpellWizard(){

ImageIcon BlueSpell = new ImageIcon("resources/bluespellnobackground.png");

return BlueSpell;
}


public static ImageIcon BadGuyWizard(){

ImageIcon BadGuy = new ImageIcon("resources/badguynobackground.png");

return BadGuy;
}


public static ImageIcon GoodGuyWizardIcon(){

ImageIcon GoodGuy = new ImageIcon("resources/goodguynobackground.png") ;



return GoodGuy;

}


public static Image createimgpng(){



BufferedImage WizardBattleIcon = null;

try{
File wizardBattleIconFile = new File("resources/goodguynobackground.png");
WizardBattleIcon = ImageIO.read(wizardBattleIconFile);
ImageIO.write(WizardBattleIcon, "png", wizardBattleIconFile);
   
}
catch(IOException e){
e.printStackTrace();
}



return WizardBattleIcon;


}

public static JLabel createimgjpg(){

BufferedImage MainMenuBackground = null;

try{
File MainMenuBackgroundFile = new File("resources/WizardBattleBackground.jpg");
MainMenuBackground = ImageIO.read(MainMenuBackgroundFile);
ImageIO.write(MainMenuBackground, "jpg", MainMenuBackgroundFile);

}
catch(IOException e){
e.printStackTrace();
}

        WizardBackground = new JLabel(new ImageIcon(MainMenuBackground));


return WizardBackground;



}






public static void MainMenu(){


Window.setSize(640, 480);
Window.add(MainMenuButtons);
Window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Window.setLocationRelativeTo(null);
Window.setTitle("Wizard Battle");
MainMenuButtons.add(Start);
MainMenuButtons.add(Exit);
Window.getContentPane().add(MainMenuButtons, BorderLayout.SOUTH);
Window.setIconImage(createimgpng());
Window.add(createimgjpg());
Window.setVisible(true);
Window.setResizable(false);

Exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

Window.dispose();

}



});

Start.addActionListener(new ActionListener(){
@SuppressWarnings("static-access")
public void actionPerformed(ActionEvent e){

Window.setExtendedState(Window.MAXIMIZED_BOTH);
Window.setFocusable(true);
Window.remove(WizardBackground);
Window.remove(MainMenuButtons);
WizardBattleGrid.CreateGrid();
WizardKeyHandeler WizardKey = new WizardKeyHandeler();
Window.addKeyListener(WizardKey);




}
});
}
}


Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
Re: Making sprite move across screen
« Reply #1 on: March 24, 2014, 09:00:00 am »
You need a game loop.
You have two possibilities:
1. Learn how to do it yourself with e.g. this ebook: http://fivedots.coe.psu.ac.th/~ad/jg/
2. Use a game engine that does it for you. One example is: http://jmonkeyengine.org/
« Last Edit: March 24, 2014, 09:00:12 am by Deque »

Offline m1kesanders

  • Serf
  • *
  • Posts: 48
  • Cookies: -5
  • I'm sarcastic, somewhat new, and a grey hat
    • View Profile
Re: Making sprite move across screen
« Reply #2 on: March 24, 2014, 09:27:25 am »
Alright thank you i'll give it a try :)

Offline m1kesanders

  • Serf
  • *
  • Posts: 48
  • Cookies: -5
  • I'm sarcastic, somewhat new, and a grey hat
    • View Profile
Re: Making sprite move across screen
« Reply #3 on: March 24, 2014, 09:42:18 am »
Alright so I gave it a try and all it's doing is is automatically setting the icon to the end and then to null, how do I do it so it just goes up by one until it hits the edge of the grid? here's my updated code thanks :)

Code: (javascript) [Select]
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ImageIcon;


@SuppressWarnings("serial")
public class WizardKeyHandeler extends WizardBattleGrid implements KeyListener, Runnable {
public Thread BlueSpellThread = new Thread(this);
public boolean IsFinished = true;

@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();

if(key == KeyEvent.VK_W){

if(getGoodGuy().getx() != 0){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()-1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}

}

else if(key == KeyEvent.VK_S){

if(getGoodGuy().getx() != 19){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()+1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_D){

if(getGoodGuy().gety() != 9){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()+1)].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_A){

if(getGoodGuy().gety() != 0){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()-1)].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_SPACE){
if(getBlueSpell().gety() != 19){
IsFinished = false;
run();

}

}

}





@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(getGoodGuy().getx() != 0){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_S){

if(getGoodGuy().getx() != 19){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_D){

if(getGoodGuy().gety() != 9){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_A){

if(getGoodGuy().gety() != 0){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_SPACE){
for(int i=0; i<20; i++){
//IsFinished=false;
//run();
if(getBlueSpell().gety() != 19){

WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(getBlueSpell().getIcon());

}
else{
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
}
}

}





}
//}



@Override
public void keyTyped(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_W){
if(getGoodGuy().getx() != 0){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()-1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());

}

}

else if(key == KeyEvent.VK_S){

if(getGoodGuy().getx() != 19){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().changex(getGoodGuy().getx()+1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_D){

if(getGoodGuy().gety() != 9){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()+1)].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_A){

if(getGoodGuy().gety() != 0){

WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()-1)].setIcon(getGoodGuy().getIcon());
}
}

else if(key == KeyEvent.VK_SPACE){
if(getBlueSpell().gety() != 19){
IsFinished =false;
run();

}

}

}





@Override
public void run() {

while(!IsFinished){
if(getBlueSpell().gety()<19){
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
WizardCells[getBlueSpell().changex(getGoodGuy().getx())][getBlueSpell().changey(getBlueSpell().gety() + 1)].setIcon(getBlueSpell().getIcon());

try{
Thread.sleep(1);
}
catch(InterruptedException ex){
Logger.getLogger(WizardKeyHandeler.class.getName()).log(Level.SEVERE, null, ex);
}

}
else{
WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
IsFinished = true;
}


}


}
}