More SVG questions (and Javascript) - Interactivity [moving a rectangle] -


The following code should be rectangularized while pressing WASD keys on the keyboard. This does not work, the rectangle is not transferred, but no error occurs. How can I do this work?

  & lt ;? Xml version = "1.0" encoding = "UTF-8" standalone = "no"? & Gt; & Lt ;! DOCTYPE svg PUBLIC "- // W3C / DTD SVG 1.1 / n" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> & Lt; Svg width = "90%" height = "90%" version = "1.1" xmlns = "http://www.w3.org/2000/svg" & gt; & Lt; Script type = "text / javascript" & gt; & Lt ;! [CDATA [var x; Var y; Function moves () {x = new number (document.getElementById ("player").); Y = new number (document.getElementById ("player"). Y); Switch (event.keyCode) {Case 119: y--; Document.getElementById ("player") y = y break; Case 115: y ++; Document.getElementById ("player") y = y break; Case 97: X-; Document.getElementById ("player") x = x break; Case 100: X ++; Document.getElementById ("player") x = x break; Default:}} document.documentElement.addEventListener ("keypress", move, right); ]] & Gt; & Lt; / Script & gt; & Lt; Rect x = "0" y = "0" height = "100%" width = "100%" style = "stroke-width: 5; stroke: black; fill: white" & gt; & Lt; / Rect & gt; & Lt; Rect x = "250" y = "250" id = "player" height = "50" width = "50" style = "stroke-width: 1; stroke: red; fill: red" & gt; & Lt; / Rect & gt; & Lt; / Svg & gt;  

Your code above does not work because the objects are, those values ​​that represent You can change the x = new number (document.getElementById (<)

The simplest changes are to make your code 'work':

  1. Change
    x = new number (document.getElementById "player").);

    x = new number (document.getElementById ("player") .x.baseVal.value);
    (and so on)

  2. Change
    document.getElementById ("player"). X = x;


    document.getElementById ("player"). X.baseVal.value = x;
    (and so on)

    This will make your code work as desired, however, your code Usually can be improved.

    Here's how I will write it (if you really want to move only one unit at a time):

       & Lt; Script type = "text / javascript" & gt; & Lt ;! [CDATA [var key = {w: 87, a: 65, s: 83, d: 68}; Var rect = document.getElementById ('player'); Var x = rect.getAttribute ('x') * 1, y = rect.getAttribute ('y') * 1; Document.documentElement.addEventListener ("keydown", function (evt) {switch (evt.keyCode) {case key.av: rect.y.baseVal.value = --y; break; case KEY.s: rect.y. BaseVal.value = ++ y; Pause; Case KEY.a: rect.x.baseVal.value = --x; break; Case KEY.d: rect.x.baseVal.value = ++ x; break;}} , false); ]] & Gt; & Lt; / Script & gt; & Lt; / Svg & gt;   

    Note that I did not sign in to & lt; Rect & gt; above & lt; Script & gt; The block has been moved so that a) the user looks at the rectangle before loading the script, but more importantly it is that b) a code which calls to getElementById () Existed before If your script is blocked before defining your elements, then you have to set your code reference in response to the onload or similar event handler.

Comments

Popular posts from this blog

qt - switch/case statement in C++ with a QString type -

python - sqlite3.OperationalError: near "REFERENCES": syntax error - foreign key creating -

Python's equivalent for Ruby's define_method? -