Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem z klasami
#1
Dzień dobry!
Mam pewien problem. Mianowicie klasa u mnie nie działa tak, jak powinna działać. Jeśli przypiszę klasę do jakiejś zmiennej, to cały czas operuję przez zmienną na klasie, tak jakby była tablicą.
Dziękuję uprzejmie z góry za pomoc Smile

Code:
local fst;
local sec;
local thr;

class window
{
    config = {
        x = 0,
        y = 0,
        width = 0,
        height = 0,
        texture_name = "",
        enabled = false
    };
    
    objects = {
        texture = null,
        count = 0
    };

    dotestu = 0;
    
    constructor(x,y,width,height,texture_name)
    {
        dotestu++;
        config.x = x;
        config.y = y;
        config.width = width;
        config.height = height;
        config.texture_name = texture_name;
        
        objects.texture = Texture(x,y,width,height,texture_name);
    }
    
    function createText(text, x, y)
    {
        objects.count++;
        objects[objects.count] <- {};
        objects[objects.count].draw <- Draw(x + config.x, y + config.y,text);
        objects[objects.count].draw.setColor(255,255,255);
        objects[objects.count].draw.font = "FONT_OLD_10_WHITE_HI.TGA";
    }
    
    function test()
    {
        Chat.print(255,0,0,dotestu);
    }
    
    function show()
    {
        config.enabled = true;
        objects.texture.visible = true;
        
        if (objects.count > 0) {
            for (local i = 1; i <= objects.count; ++i) {
                objects[i].draw.visible = true;
            }
        }
    }
    
    function hide()
    {
        config.enabled = false;
        objects.texture.visible = false;
        
        if (objects.count > 0) {
            for (local i = 1; i <= objects.count; ++i) {
                objects[i].draw.visible = false;
            }
        }
    }
}

function onInit()
{    
    fst = window(0,0,5000,2000,"DLG_CONVERSATION.TGA");
    fst.createText("ABCD", 100, 100);
    fst.createText("BCDE", 100, 300);
    fst.createText("CDEF", 100, 500);
    fst.createText("DEFG", 100, 700);
    fst.test();
    
    sec = window(0,3000,5000,2000,"DLG_CONVERSATION.TGA");
    sec.createText("EFGH", 100, 100);
    sec.createText("FGHI", 100, 300);
    sec.test();
    
    thr = window(0,6000,5000,2000,"DLG_CONVERSATION.TGA");
    thr.createText("GHIJ", 100, 100);
    thr.createText("HIJK", 100, 300);
    thr.createText("XXXX", 100, 500);
    thr.test();
    
    fst.show();
    sec.show();
    thr.show();
    
    for (local i = 1; i <= thr.objects.count; ++i) {
        Chat.print(255,0,0,thr.objects[i].draw.text);
    }
}

addEventHandler("onInit", onInit);
Reply
#2
Bo twoje pola są statyczne. Jeśli masz pole, które jest tablicą lub tabelą i jest tak po prostu zdefiniowana w klasie to każda instancja klasy ma ten sam adres tych obiektów, więc de fakto operujesz na tych samych danych. Nie robi się tak, powinieneś config oznaczyć jako null i tworzyć tą tabele w konstruktorze, wtedy każdy obiekt będzie miał swoją własną instancję tej tabeli.
Reply
#3
Więc tak to działa. Dziękuję za pomoc serdecznie Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem z nową wersją Budyń 8 7,318 18.11.2018, 15:55
Last Post: Budyń
  Problem z własnym serwerem Zenko 2 3,211 01.03.2018, 23:13
Last Post: Profesores
  Problem z myszką Pilar 3 3,495 19.02.2018, 02:54
Last Post: Pilar
  Problem z wbiciem na serwer Dyamiti 8 8,929 18.02.2018, 22:35
Last Post: Dyamiti
  Problem buuuuuuxd 4 4,031 09.01.2018, 17:57
Last Post: buuuuuuxd
  Problem z włączaniem serwera na Ubuntu Mattwell 2 3,139 06.01.2018, 19:55
Last Post: Mattwell
  Problem z włączeniem g2o razor_take 9 7,751 26.07.2017, 00:40
Last Post: KimiorV
  Problem z rozdzielczością Aleksiej 3 3,419 15.07.2017, 14:37
Last Post: Aleksiej
  Problem G2O_Proxy.dll! Kod błędu 126 Jankoqaz 1 2,222 14.07.2017, 22:46
Last Post: Quarchodron
  Problem z odpaleniem servera na g2o Jankoqaz 5 4,888 14.07.2017, 08:14
Last Post: Marcel

Forum Jump:


Users browsing this thread: