Skocz do zawartości

Rekomendowane odpowiedzi

Opublikowano

Jako że jestem noobem, przykład pola tekstowego wziąłem sobie z internetu.

 

A więc mam dwa obiekty: obj_input i obj_log.

Log jest tylko cieniem wpisanego tekstu, więc chyba nie będzie tu potrzebny.

 

Problem polega na tym, iż nie wiem jak sprawić by po wpisanu tylko jakiegoś odpowiedniego wyrażenia, np. "Hello" działa się jakaś akcja.

 

Create z obj_input:

GML (create)
caret_flash = 0

caret_flash_rate = 15

caret_move = 0

caret_move_rate = 7

text = "" // current text

caret = 0 // caret position

fillchar = "`" // used as filler character in input handling.

filltext = string_repeat(fillchar, 10)

 

Step z obj_input:

GML (step)
// keyboard shortcuts:

caret_move = max(0, caret_move - 1)

if (keyboard_check(vk_left) != keyboard_check(vk_right)) {

if (caret_move == 0) {

if (keyboard_check(vk_left)) {

if (caret > 0) caret -= 1

} else {

if (caret < string_length(text)) caret += 1

}

caret_move = caret_move_rate

}

} else caret_move = 0

if (keyboard_check_pressed(vk_home)) caret = 0

if (keyboard_check_pressed(vk_end)) caret = string_length(text)

if (keyboard_check_pressed(vk_delete)) {

text = string_delete(text, caret + 1, 1)

}

// actual input:

if (keyboard_string != filltext && keyboard_string != "") {

var n, l, t;

t = keyboard_string

l = string_length(t)

n = 1

// find where actual input starts

while ((n <= l) && (string_char_at(t, n) == fillchar)) n += 1

c = 11 - n

if (c > 0 && caret > 0) {

caret -= c

text = string_delete(text, caret + 1, c)

}

c = l - n + 1

if (c > 0) {

text = string_insert(string_copy(t, n, c), text, caret + 1)

caret += c

}

}

keyboard_string = filltext

caret = max(0, min(caret, string_length(text)))

Draw z obj_input:

GML (draw)
draw_set_font(LCD)

draw_set_color(make_color_rgb(0, 245, 57))

var i, h, ptext;

// sad truth: There is no way you can display the typed "#"

// characters and get string width of those.

ptext = string_replace_all(text, '#', '¹')

draw_text(x, y, ptext)

// add those useless fancy lines below the input:

i = string_width(string_copy(ptext, 1, caret))

h = string_height('\\')

draw_line(x + i, y, x + i, y + h)

i = string_width(text)

draw_line(x - 3, y + h + 2, x + i + 2, y + h + 2)

draw_line(x - 3, y + h + 4, x + i + 10, y + h + 4)

// key handling:

if (keyboard_check_pressed(vk_enter)) {

with (obj_log) {

for (i = length - 1; i >= 0; i -= 1) text[i + 1] = text

length = min(maxlength, length + 1)

text[0] = ptext

}

text = ''

caret = 0

}

Opublikowano

kdzie masz definowane:

length

text

 

akcja sie bedzie dziac(po wcisnieciu ENTER), ale z tego co pokazujesz skoczy sie bledem :)

Opublikowano

przepraszam :)

 

1, kdzie masz definowane ?

- length

- text

 

2a, stwierdzam: akcja sie bedzie dziac(po wcisnieciu ENTER)

2b, tlumacze dlaczego pytam v punktu 1, : ale z tego co pokazujesz skoczy sie bledem

 

3a, chyba JA zle przeczytalem twe pytanie.

jesli chodzilo ci by akcja byla wykonana po napisaniu unikalnego slowa to mozesz porownywac stringi.

GML
if (_string_zadany_w_input == _string_szukany) && (keyboard_check_pressed(vk_enter)) ) { /* wykonaj akcje*/ };

 

3b, ten sam sposob jako v 3a, ale inne wykonanie. szuka dokladnie ten sam string. naprz jesli "hello world" jest w "hello" wynik bedzie false

GML
if (string_pos(_string_zadany_w_input, _string_szukany) && (keyboard_check_pressed(vk_enter)) ) { /* wykonaj akcje*/ };

// string_pos wraca 0/false kdy nic nieznalazl</span>

 

3c, tak jak v 3b, ale tym razem szuka jesli string jest wogole w zadanym stringu. naprz jesli "hello" jest w "hello world" wynik bedzie true

GML
if (string_pos( _string_szukany, _string_zadany_w_input) && (keyboard_check_pressed(vk_enter)) ) { /* wykonaj akcje*/ };

// string_pos wraca 0/false kdy nic nieznalazl</span>

Opublikowano

"_string_zadany_w_input" jest twa zmienna, ktora przechowuje string tobo napisany string do boxu

 

naprzyklad:

GML
_string_szukany="hello";

_string_zadany_w_input=get_string("napisz hello","tu"); // tu dasz swoj sposob pozyskania napisanego stringu

if (_string_zadany_w_input == _string_szukany) && (keyboard_check_pressed(vk_enter)) ) {

/* wykonaj akcje*/

show_message( 'napisalesz "hello" ' );

};

Jeśli chcesz dodać odpowiedź, zaloguj się lub zarejestruj nowe konto

Jedynie zarejestrowani użytkownicy mogą komentować zawartość tej strony.

Zarejestruj nowe konto

Załóż nowe konto. To bardzo proste!

Zarejestruj się

Zaloguj się

Posiadasz już konto? Zaloguj się poniżej.

Zaloguj się
  • Ostatnio przeglądający   0 użytkowników

    • Brak zarejestrowanych użytkowników przeglądających tę stronę.
×
×
  • Dodaj nową pozycję...