setNode question
tupper

Hi all!

I am new to Wade and am playing around with some of its features.

I used the path tool to create multiple paths and would like to change the duration of each node such that my sprite moves along all pathes at the same constant speed independant of how long they are. (To achieve this more easily, an alternative option besides "duration" would probably be good, something like "speed"?).

To do this, I think I need to calculate the length of each path and take into account my desired speed to get the duration I have to set for each node. I haven't done this yet, I at first tried to find out how to set the duration via code.

At the documentation there is a function "setNode".
However, for me it only says "The data to set for the node. This is an object that can contain any of the following fields (all optional):
Where properties is an object that can contain any number of custom fields."
and not the actual fields which are allowed.

I tried the following in the "onPathNode" function of my sprite which I want to move along the path: 

var node = {duration:0.9};
this.getPath().setNode(0,node);

var node = {duration:0.9};
this.getPath().setNode(1,node);

My idea is that I want to update the "duration" field of node 0 and 1, but leave everything else as it is of node 0 and 1.
However, my sprite does not move along the path anymore. How can this be done?

An idea for improvement: Why is the "nodeIndex" argument inside "setNode" function? Wouldn't it be cleaner to have a set of setX(...) functions which can be called on the node retrieved via the"getNodeRef" function where X are the properties like "duration"?
(Edit: Maybe it's not really cleaner, but just what I am used to? :))

In the documentation there seems to be only "getPath()" (get the active path).
Wouldn't it be nice to have function which returns all pathes in the scene to iterate over them to set the desired values? This could then be done in the App function.
Another thing which just comes to my mind: If I e.g. misspell a function name, the game does not load anymore. Is there a way to get feedback / debug what lines caused the error inside the web IDE?

That's all for the moment :) It would be great to get feedback!

All 5 Comments
Gio

Hi tupper

Admittedly all the Path stuff is a very recent addition to WADE and the interface could be better. We will try to improve it for the next version and your suggestions are much appreciated, especially the "speed" thing - it would make sense to have a setSpeed function there.

Nodes are simple data structures where all the properties are publicly accesible, so if you want to change the duration of a node, you should be able to just do this:

myPath.getNodeRef(0).duration = 0.1;

There is, in fact, a wade.getPaths() function to get all paths in the scene - but it seems that I forgot to add it to the documentation. The syntax is identical to wade.getSceneObjects(). Noted, I will fix that for the next release too.

Finally, the best way to see what's stopping your game from running is looking in the browser's console (F12). Ideally we should find a way to grab this information and display it inside the IDE, something that's worth investigating.

Thanks for your questions, they've highlighted a few problems and have been very useful.

tupper

Thank you Gio!

For the meantime I think I made it work like this (if others are interested :)):

App = function()
{
	this.init = function()
	{
	    speed = 150;
	    
		wade.loadScene('scene1.wsc', true, function()
        {
            pathsArray = wade.getPaths();
            
            for (var i = 0; i < pathsArray.length; i++) {
                path = pathsArray[i];
             
                for (var j = 1; j < path._nodes.length; j++) {
                    node1 = path._nodes[j-1];
                    node2 = path._nodes[j];
                    node1.duration = calcDurationForSpeed(node1, node2, speed);
                }
            }
            
            function calcDurationForSpeed(node1, node2, speed) {
                x_diff = Math.abs(node1.position.x) - Math.abs(node2.position.x);
                y_diff = Math.abs(node1.position.y) - Math.abs(node2.position.y);
                hypothenuse = Math.sqrt(x_diff * x_diff + y_diff * y_diff);
                return hypothenuse / speed;            
            }
        });
	};
};

It certainly has an effect and I think it is correct, maybe it's just an optical illusion that it's not 100% excatly the same speed. Can anyone find a bug maybe?

Another thing I came across: Would it be good to have a "this.getPath("not-the-current-path") function? At the moment I have to write a function to loop over all pathes to get the desired one:

function getPath(pathName) {
    pathsArray = wade.getPaths();
    var path = null;
        
    for (var i = 0; i < pathsArray.length; i++) {
        var currPath = pathsArray[i];
        if (currPath._name == "pfad_0_umgekehrt") 
        {
            path = pathsArray[i];
            break;
        }
    }
    
    return path;
}

One last thing (should I open additional threads for these things next time?): 
In my "onPathNode" function of my sprite I have the following:

if(this.getPath().getName() == "path_0")
{
    if(data.nodeIndex == 1)
    {
        if(wade.isKeyDown(38))
        {
            this.setPath("path_1");
        }
    } 
}

All my pathes consist of two nodes.  When the "up key" (code 38) is pressed when the last node is reached, my sprite switches the path. This works great. However, If I miss pressing the key before and do so after I have reached my last node, this does not trigger anymore (I guess the "onPathNode" event fires only once?).

So I tried the following in the "onKeyDown" function:

if(this.getPath().getName() == "path_0")
{
    if(wade.isKeyDown(38) && data.nodeIndex == 1)
    {
        this.setPath("path_1");
    } 
}
 

However, nothing happens. This is probably because I don't have access to "data.nodexIndex" here, do I? Could you tell me how to work around this?

Edit: I don't know why it's not highlighted the Javascript way, I edited this afterwards. Sorry!

Gio

It looks good, however there is a wade.getPath() function where you can pass in the path name and it returns the corresponding Path object. Again, this needs to be added to the documentation, but it does exist in the current version already.

I would also note that there is a more compact version of writing your calcDurationForSpeed function above, using the wade.vec2 functions:

function calcDurationForSpeed(node1, node2, speed) 
{
	return wade.vec2.length(wade.vec2.sub(node1.position, node2.position)) / speed;            
}

Regarding your other question - you are right, the data for the onKeyDown event does not include any information about the path (such as the current nodeIndex). You probably want to set a flag on the object once the path is complete, so in the onPathNode event:

if(this.getPath().getName() == "path_0")
{
    if (data.nodeIndex == 1)
    {
        if(wade.isKeyDown(38))
        {
            this.setPath("path_1");
        }
        this.path0IsComplete = true; 
    }
}

So in your onKeyDown event you can just check if this.path0IsComplete is true.

tupper

Thank you, Gio! It's really great to receive such fast and helpful replies.
 

tupper

Feature request regarding the "speed" idea:
It would be really cool if one could define for a given path for each different sprite (traveling along this path) different, individual speeds! Otherwise each sprite has to move at the same speed, or, to work around this, I think one had to overlay multiple paths (one for each different speed) [I think it's not possible to change the speed on the fly as necessary because two sprites with different individual speeds should be allowed to be on the same path] which is not so nice. 

Post a reply
Add Attachment
Submit Reply
Login to Reply