egy hazafi Creative Commons License 2017.10.30 0 0 1099

És abban tudnátok segíteni-elmagyarázni, hogy az Invalidate függvény hívása miért nem működik egy saját, létrehozott Class-on belül? Most már létre tudom hozni objektumként a játékosnégyzetet, ki is tudom így rajzoltatni az osztályból, abban van az elmozdulás ellenőrzése is, de az Invalidate-et nem tudom benne használni, így az elmozdulás nem látszik.

Így néz ki a mainform-om:

 

using System; 
using System.Drawing; 
using System.Collections.Generic; 
using System.Windows.Forms; 

namespace teglalap 

    public partial class MainForm : Form 
    { 
       

       public MainForm() 
        { 
            InitializeComponent(); 
            player p1=new player(50,50);
            Paint += new PaintEventHandler(p1.rajzol); 
            KeyUp += new KeyEventHandler(p1.gombFel);
            KeyDown += new KeyEventHandler(p1.gombLe);
        }
        void MainFormLoad(object sender, EventArgs e)
        {
    
        }

    }

}

 

így pedig a class1 forráskódja:

 

using System;
using System.Drawing;
using System.Collections.Generic;
using System.Windows.Forms; 

namespace teglalap
{
    /// <summary>
    /// Description of Class1.
    /// </summary>
    public class player
    {
        protected int x=50;
        protected int y=50;
        protected Boolean jobbranyil=false;
        protected Boolean balranyil=false;
        protected Boolean felnyil=false;
        protected Boolean lenyil=false;
        
        public player()
        {
        }
        
        public player(int x, int y){
            this.x=x;
            this.x=y;
        }
        
        public void setHelyx(int x){
            this.x=x;
        }
        
        public int getHelyx(){
            return x;
        }
        
        public void setHelyy(int y){
            this.y=y;
        }
        
        public int getHelyy(){
            return y;
        }
        
        public void rajzol(object sender, System.Windows.Forms.PaintEventArgs e){
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            e.Graphics.FillRectangle(blueBrush,x,y,50,50);
            
            
        }
        
        public void gombLe(object sender, System.Windows.Forms.KeyEventArgs e){
            if (e.KeyCode==Keys.Left
            { 
                balranyil=true;
            } 
            if (e.KeyCode==Keys.Right
            { 
                jobbranyil=true
            } 
            if (e.KeyCode==Keys.Up
            { 
                felnyil=true
            } 
            if (e.KeyCode==Keys.Down
            { 
                lenyil=true
            }
            
            mozg(sender, e);
        }
        
        public void gombFel(object sender, System.Windows.Forms.KeyEventArgs e){
            
            if (e.KeyCode==Keys.Left
            { 
                balranyil=false;
                
            } 
            if (e.KeyCode==Keys.Right
            { 
                jobbranyil=false
            } 
            if (e.KeyCode==Keys.Up
            { 
                felnyil=false
            } 
            if (e.KeyCode==Keys.Down
            { 
                lenyil=false
            } 
            mozg(sender, e);
        }
        
        public void mozg(object sender, System.Windows.Forms.KeyEventArgs e){

            if (jobbranyil==true
            { 
                x=x+5;
            } 
            if (balranyil==true
            { 
                x=x-5;
            } 
            if (felnyil==true
            { 
                y=y-5;
            } 
            if (lenyil==true
            { 
                y=y+5;
            }
             Invalidate(); //De valamiért nem jó!!!
        }
    }
}