Closed tyilo closed 11 years ago
I believe it's correct as it is. It automatically creates a group based on z-index. In your case it would be enough to use io.addToGroup('obj1',obj1); instead of first using io.addObj(obj1); io.addToGroup('obj1',obj1);
iio seems to automatically use addObj when calling addToGroup();
ioAppManager.prototype.addToGroup = function(tag, obj, zIndex, c){
c=c||0;
var i = this.indexOfTag(tag, c);
var a = iio.isNumber(i);
if (typeof(this.cnvs[c].groups)=='undefined'||!a)
i = this.addGroup(tag, zIndex, c);
this.cnvs[c].groups[i].addObj(obj, c); // calls addObj by default
return obj;
}
ioAppManager.prototype.addObj = function(obj, zIndex, c){
c=c||0;
if (typeof(this.cnvs[c].groups)=='undefined') this.cnvs[c].groups = [];
zIndex = zIndex || 0;
for (var i=0; i<this.cnvs[c].groups.length; i++)
if (this.cnvs[c].groups[i].zIndex == zIndex){
this.cnvs[c].groups[i].addObj(obj);
if (typeof this.fps == 'undefined' && typeof obj.pos!='undefined')
obj.draw(this.ctxs[c]);
return obj;
}
this.addGroup(zIndex, zIndex, c);
this.addToGroup(zIndex, obj, zIndex, c);
if (typeof this.fps == 'undefined' && typeof obj.pos!='undefined')
obj.draw(this.ctxs[c]);
return obj;
}
My code works now, but shouldn't you be able to add an object without it having a group?
Having a single management structure simplifies the architecture. The functions 'addObj' and 'rmvObj' are there to allow you to pretend the group structure doesn't exist. All the group does is add one more pointer between the array of objects and the functions that access it, so I don't think the phantom group should effect efficiency.
I may be wrong about that though - let me know if my reasoning is incorrect, it would be pretty easy to add this option, but the code would get a bit messier.
I don't see why the code I posted in the first post shouldn't work. I now know why it doesn't, but I think it is wrong to just add an object to the first group with the required z-index if the group isn't specified.
At least if you want to keep it that way, could every ioAppEngine start out with an empty group called _default
(or something else) with a z-index at 0?
That way every object created with just io.addObj
would be added to the _default
group, which shouldn't normally have any collision detection going on.
Wait, I think there might be some confusion. This code
io.addObj(obj1);
io.addToGroup('obj1', obj1);
is redundant - 'addToGroup' calls 'addObj' for you, and if no group exists, 'addObj' will call 'addToGroup' for you.
If you don't want to deal with groups, just call this:
io.addObj(obj1);
and a default group at zIndex 0 with the tag 0 will be created automatically.
The problem is that if I do this:
var cake = io.addToGroup('cakes', new iio.ioRect());
// Tons of unrelated code...
var notCake = io.addObj(new iio.ioText());
Now notCake
will be in the group cakes
, even though I didn't specify the group for notCake
!
That's true. I have altered 'addObj' to only add the given obj to an existing group if group's tag is 0 (Number 0). The update has been merged with the git distribution, your code should work with it.
When running the following app's code it will fail to run:
If you, however, moving the text creating code to the top of the function will work:
Also adding
io.addGroup('text');
before the text creation will work.It seems as if iio defaults the group of an object to the latest group defined. It should instead assign the text object to the undefined group, where it would be if the code was executed at the beginning of the function.