Torna indietro   Hardware Upgrade Forum > Software > Programmazione

Recensione HONOR Magic V6: spessore record e super batteria. È lui il fold da battere?
Recensione HONOR Magic V6: spessore record e super batteria. È lui il fold da battere?
HONOR Magic V6 è arrivato in Italia a 2.299,90 euro con una promessa precisa: unire 4 mm di spessore da aperto (8,75 mm chiuso nel modello White, 9 mm negli altri colori) a una batteria da 6.660 mAh, la più capiente mai vista su un pieghevole. Lo abbiamo usato per oltre una settimana in versione Red 16/512 GB per capire se lo Snapdragon 8 Elite Gen 5 tiene testa alla concorrenza anche fuori dai benchmark ufficiali
Redmi Pad 2 9.7: ampio display, economico e peso contenuto, ma qualche limite nelle prestazioni
Redmi Pad 2 9.7: ampio display, economico e peso contenuto, ma qualche limite nelle prestazioni
Redmi Pad 2 9.7 punta su un display ampio e fluido, una batteria capace di accompagnare l'uso quotidiano senza ansie da ricarica e un prezzo accessibile, a partire da 179,90 euro per la versione con 64 GB di storage. Lo Snapdragon 6s 4G Gen 2 e i 4 GB di RAM della configurazione più diffusa frenano però chi cerca reattività e multitasking spinto: ecco il bilancio dopo due settimane di prova diretta
Peugeot Polygon Concept: ecco il futuro delle utilitarie
Peugeot Polygon Concept: ecco il futuro delle utilitarie
Polygon è la concept car di Peugeot che mostra il futuro delle soluzioni del segmento B: tra design compatti e innovativi affiancati da dimensioni compatte uno scherzo dalla manovrabilità incredibile per le manovre a bassa velocità
Tutti gli articoli Tutte le news

Vai al Forum
Rispondi
 
Strumenti
Old 30-06-2012, 16:24   #1
Briliant
Member
 
Iscritto dal: Oct 2006
Messaggi: 65
[Java] Problema con lancio nuovo livello in un gioco.

Allora, sto' programmando un piccolo shooter 2D in java. Ho creato due classi, Stage (Dove serve ammazzare 20 alieni) e Boss (Dove si spara alla nave madre).

Entrambe le classi funzionano in maniera perfetta se lanciate dal main, se invece lancio Boss da Stage, funziona tutto tranne le animazioni per la nave.

Boss
Codice:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;


public class Stage extends JPanel implements ActionListener, VariabiliComuni {

    protected Timer timer;
    protected R9XA craft;
    protected ArrayList aliens;
    protected boolean ingame;
    protected boolean victory;
    protected boolean destroyed;
    //protected int earthealth;
    public int earthealth = earth;
    public float damage = (earthealth/100);
   
    

    public Stage() {

        addKeyListener(new Stage.TAdapter());
        setFocusable(true);
        setBackground(Color.BLACK);
        setDoubleBuffered(true);
        ingame = true;
        victory = false;
        destroyed = false;
        
        setSize(StageW, StageH);

        craft = new R9XA();

        initAliens();

        timer = new Timer(5, this);
        timer.start();
    }

   

    public void initAliens() {
        aliens = new ArrayList();

        for (int i=0; i<AttackMatrix.length; i++ ) {
            aliens.add(new Bydo(AttackMatrix[i][0], AttackMatrix[i][1]));
        }
    }


    public void paint(Graphics g) {
        super.paint(g);

        if (ingame) {

            Graphics2D g2d = (Graphics2D)g;

            if (craft.isVisible())
                g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(),
                              this);

            ArrayList ms = craft.getBurst();

            for (int i = 0; i < ms.size(); i++) {
                Beam m = (Beam)ms.get(i);
                g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
            }

            for (int i = 0; i < aliens.size(); i++) {
                Bydo a = (Bydo)aliens.get(i);
                
                if (a.isVisible())
                    g2d.drawImage(a.getImage(), a.getX(), a.getY(), this);
                
            }

            g2d.setColor(Color.WHITE);
            g2d.drawString("Aliens left: " + aliens.size(), 5, 15);
            g2d.drawString("Earth Energy:" + earthealth, 5, 35);
            }


         else {
            if ((victory == false) &&
               (destroyed == false))     {
            String msg = "You got destroyed! Failed to protect Earth!";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (StageW - metr.stringWidth(msg)) / 2,
                         StageH / 2);
            }
        
        
        
            else if ((victory == false) &&
               (destroyed == true))     {
            String msg = "Earth got destroyed! Mankind has no more an Home!";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (StageW - metr.stringWidth(msg)) / 2,
                         StageH / 2);
            }
        
            else if ((victory == true) &&
               (earthealth < 2500))     {
            String msg = "Too much damage to Earth! Mankind has become extinct! ";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (StageW - metr.stringWidth(msg)) / 2,
                         StageH / 2);
            }
        
            
         else {
                String msg = "Invasion Repelled";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (StageW - metr.stringWidth(msg)) / 2,
                         StageH / 2);
            
                
            }
            
        }

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }


    public void actionPerformed(ActionEvent e) {
        
         //if (aliens.size()==0) {
            
           if (aliens.size() < 20) {
           //victory = true;
           //ingame = false;
            
            craft.setVisible(false);
            timer.stop();
            ingame = false;
            //craft.setVisible(false);
            
            add(new Boss());
            
            
            
        }
        
        if (earthealth < 0) {
            destroyed = true;
            ingame = false;
        }

        ArrayList ms = craft.getBurst();

        for (int i = 0; i < ms.size(); i++) {
            Beam m = (Beam) ms.get(i);
            if (m.isVisible()) 
                m.move();
            else ms.remove(i);
        }

        for (int i = 0; i < aliens.size(); i++) {
           Bydo a = (Bydo) aliens.get(i);
           
                          
                
           
            if (a.isVisible()) 
            {
                a.move();
                if (a.getX() < 40)
                earthealth--;
            }
            else aliens.remove(i);
            
        }
        
       craft.move();
        checkCollisions();
        repaint();  
    }


    public void checkCollisions() {

        Rectangle r3 = craft.getBounds();

        for (int j = 0; j<aliens.size(); j++) {
            Bydo a = (Bydo) aliens.get(j);
            Rectangle r2 = a.getBounds();

            if (r3.intersects(r2)) {
                craft.setVisible(false);
                a.setVisible(false);
                ingame = false;
            }
            
            
        }

        ArrayList ms = craft.getBurst();

        for (int i = 0; i < ms.size(); i++) {
            Beam m = (Beam) ms.get(i);

            Rectangle r1 = m.getBounds();

            for (int j = 0; j<aliens.size(); j++) {
                Bydo a = (Bydo) aliens.get(j);
                Rectangle r2 = a.getBounds();

                if (r1.intersects(r2)) {
                    m.setVisible(false);
                    a.setVisible(false);
                }
            }
        }
    }
    
    public int getDamage() {
        return earthealth;
    } 

    


    public class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            craft.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            craft.keyPressed(e);
        }
    }
}
Boss

Codice:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;


public final class Boss extends JPanel implements ActionListener, VariabiliComuni {

    protected Timer timer;
    protected R9XA craft;
    protected Mothership ship;
    protected ArrayList aliens;
    protected boolean ingame;
    protected boolean victory;
    protected boolean destroyed;
    public int earthealth2 = earth;
    protected int BossHP = 100;
    
   
    

    public Boss() {

        addKeyListener(new Boss.TAdapter());
        setFocusable(true);
        setBackground(Color.BLACK);
        setDoubleBuffered(true);
        ingame = true;
        victory = false;
        destroyed = false;
        
        
        
        setSize(StageW, StageH);

        craft = new R9XA();
        ship = new Mothership();

        initAliens2();

        timer = new Timer(5, this);
        timer.start();
    }

   

    public void initAliens2() {
        aliens = new ArrayList();

        for (int i=0; i<AttackMatrix2.length; i++ ) {
            aliens.add(new Bydo(AttackMatrix2[i][0], AttackMatrix2[i][1]));
        }
    }


    
    public void paint(Graphics g) {
        super.paint(g);

        if (ingame) {

            Graphics2D g2d = (Graphics2D)g;

            if (craft.isVisible())
                g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(),
                              this);
            
            g2d.drawImage(ship.getImage(), ship.getX(), ship.getY(),
                              this);

            ArrayList ms = craft.getBurst();

            for (int i = 0; i < ms.size(); i++) {
                Beam m = (Beam)ms.get(i);
                g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
            }

            for (int i = 0; i < aliens.size(); i++) {
                Bydo a = (Bydo)aliens.get(i);
                
                if (a.isVisible())
                    g2d.drawImage(a.getImage(), a.getX(), a.getY(), this);
                
            }

            g2d.setColor(Color.WHITE);
            g2d.drawString("Mothership Energy:" + BossHP, 5, 15);
            g2d.drawString("Earth Energy:" + earthealth2, 5, 35);
            
            }


         else {
            if ((victory == false) &&
               (destroyed == false))     {
            String msg = "You got destroyed! Failed to protect Earth!";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (StageW - metr.stringWidth(msg)) / 2,
                         StageH / 2);
            }
        
        
        
            else if ((victory == false) &&
               (destroyed == true))     {
            String msg = "Earth got destroyed! Mankind has no more an Home!";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (StageW - metr.stringWidth(msg)) / 2,
                         StageH / 2);
            }
        
            else if ((victory == true) &&
               (earthealth2 < 2500))     {
            String msg = "Too much damage to Earth! Mankind has become extinct! ";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (StageW - metr.stringWidth(msg)) / 2,
                         StageH / 2);
            }
        
            
         else if (victory == true) {
                String msg = "Invasion Repelled";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (StageW - metr.stringWidth(msg)) / 2,
                         StageH / 2);
            
                
            }
            
        }

        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }


    
    public void actionPerformed(ActionEvent e) {

        if (BossHP < 0) {
           victory = true;
           ingame = false;
            
            
        }
        
        if (earthealth2 < 0) {
            destroyed = true;
            ingame = false;
        }

        ArrayList ms = craft.getBurst();

        for (int i = 0; i < ms.size(); i++) {
            Beam m = (Beam) ms.get(i);
            if (m.isVisible()) 
                m.move();
            else ms.remove(i);
        }

        for (int i = 0; i < aliens.size(); i++) {
           Bydo a = (Bydo) aliens.get(i);
           
                          
                
           
            if (a.isVisible()) 
            {
                a.move2();
                if (a.getX() < 40)
                earthealth2--;
            }
            else aliens.remove(i);
            
        }

       craft.move();
        checkCollisions();
        repaint();  
    }

    public void checkCollisions() {

        Rectangle r3 = craft.getBounds();

        for (int j = 0; j<aliens.size(); j++) {
            Bydo a = (Bydo) aliens.get(j);
            Rectangle r2 = a.getBounds();

            if (r3.intersects(r2)) {
                craft.setVisible(false);
                a.setVisible(false);
                ingame = false;
            }
            
            
        }

        ArrayList ms = craft.getBurst();
        
        Rectangle r4 = ship.getBounds();

        for (int i = 0; i < ms.size(); i++) {
            Beam m = (Beam) ms.get(i);

            Rectangle r1 = m.getBounds();

            for (int j = 0; j<aliens.size(); j++) {
                Bydo a = (Bydo) aliens.get(j);
                Rectangle r2 = a.getBounds();

                if (r1.intersects(r2)) {
                    m.setVisible(false);
                    a.setVisible(false);
                }
                
                if (r1.intersects(r4)) {
                    m.setVisible(false);
                    BossHP--;
                }
            }
        }
    }

    


    public class TAdapter extends KeyAdapter {

        @Override
        public void keyReleased(KeyEvent e) {
            craft.keyReleased(e);
        }

        @Override
        public void keyPressed(KeyEvent e) {
            craft.keyPressed(e);
        }
    }
}
Sinceramente non so' proprio cosa sbaglio.

*Edit*

Per spiegare meglio, sto' cercando una maniera per azzerare l'istanza di Stage prima di lanciare Boss.

Ultima modifica di Briliant : 01-07-2012 alle 09:09.
Briliant è offline   Rispondi citando il messaggio o parte di esso
Old 02-07-2012, 04:36   #2
Briliant
Member
 
Iscritto dal: Oct 2006
Messaggi: 65
*update*

Credo che la soluzione sia nel keylistener, in pratica quando lancio la nuova classe il jpanel viene "ridisegnato" nel JFrame (Fin qui tutto a posto, era quello che volevo) ma il KeyListener va' in conflitto con quello precedente.

Tanto e' che lanciando una classe NextLevel che mi apre lo stage 2 in una nuova finestra praticamente la prima si blocca e sulla seconda i comandi funzionano tranquillamente.

Altrimenti non ho la minima idea di cosa possa essere l'errore.
Briliant è offline   Rispondi citando il messaggio o parte di esso
 Rispondi


Recensione HONOR Magic V6: spessore record e super batteria. È lui il fold da battere? Recensione HONOR Magic V6: spessore record e sup...
Redmi Pad 2 9.7: ampio display, economico e peso contenuto, ma qualche limite nelle prestazioni Redmi Pad 2 9.7: ampio display, economico e peso...
Peugeot Polygon Concept: ecco il futuro delle utilitarie Peugeot Polygon Concept: ecco il futuro delle ut...
Reno16 Pro: il compatto di OPPO punta su fotocamera da 200MP e il nuovo Bubble! La recensione Reno16 Pro: il compatto di OPPO punta su fotocam...
 Hisense 55U7SE: tuttofare e accessibile, il MiniLED per film, sport e gioco Hisense 55U7SE: tuttofare e accessibile, il Min...
Meta AI non userà più le f...
Stop ai giochi fisici su PlayStation: pe...
NVIDIA ha creato CUDA, ma una startup vu...
Il tallone d'Achille dei QD-LED è...
Apple pronta a produrre alcuni chip con ...
Anthropic proroga ancora Fable 5 gratis ...
SBS annuncia la nuova gamma Laptop con t...
Gli italiani sono interessati alla sicur...
Galaxy Watch 9, chip più veloce f...
Steam ha appena chiuso un semestre molto...
I nuovi iPhone 18 Pro potrebbero essere ...
Huawei realizzerà chip di nuova g...
La JAXA ha eseguito un test del prototip...
Netflix, ritorna la prova gratuita per i...
Il satellite LINK per salvare il telesco...
Chromium
GPU-Z
OCCT
LibreOffice Portable
Opera One Portable
Opera One 106
CCleaner Portable
CCleaner Standard
Cpu-Z
Driver NVIDIA GeForce 546.65 WHQL
SmartFTP
Trillian
Google Chrome Portable
Google Chrome 120
VirtualBox
Tutti gli articoli Tutte le news Tutti i download

Strumenti

Regole
Non Puoi aprire nuove discussioni
Non Puoi rispondere ai messaggi
Non Puoi allegare file
Non Puoi modificare i tuoi messaggi

Il codice vB è On
Le Faccine sono On
Il codice [IMG] è On
Il codice HTML è Off
Vai al Forum


Tutti gli orari sono GMT +1. Ora sono le: 07:58.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Served by www3v