Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,157 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,026 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



onMouseMove problems when using xml

 
Reply to this topicStart new topic

onMouseMove problems when using xml

BetaWar
post 23 Jun, 2008 - 02:43 PM
Post #1


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,223



Thanked 38 times

Dream Kudos: 725
My Contributions


Okay, I am using some actionscript (2.0, Flash 8) to attach a few movieclipsto the stage and then load an image into each one of them. After that I tell it to do a mouse follow, but it doesn't seem to work (works if I don't load the image into the movie clip, but then that is defeating the point).

Okay, here is my code:

{attach movieclips and load the image}
CODE
var xml = new XML();
xml.ignoreWhite();
xml.onLoad = function() {
    var nodes = this.firstChild.childNodes;
    totalItems = nodes[1].childNodes.length-1;
    for (var i = 1; i<totalItems; i += 2) {
        var mc = main_mc.attachMovie("icon", "item_"+i, 100+i);
        mc.loadMovie(nodes[1].childNodes[i].attributes.url);
        mc.id = i;
        mc.onMouseMove = follow_mouse;
    }

}
xml.load(xml_path);


xml_path is the path to my xml document that holds the imaegs.
As I said, this part works fine, it loads the images and everything.

Now we get to this part that is making me upset.

I use this AS to get the mouse positiona and (it is supposed to) move the movieclip to its proper coordinates:

CODE
function follow_mouse(){
    id = this.id;
    mx = _xmouse;
    my = _ymouse;
    this._x = mx+(this._width*id);
    this._y = my+(this._height*id);
}


For some reason it works when I just have the standard movieclip (which consists of a black box place holder), but doesn't when I attach the image.

Anyone know what is causing this?
User is online!Profile CardPM

Go to the top of the page


girasquid
post 23 Jun, 2008 - 02:57 PM
Post #2


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,156



Thanked 6 times

Dream Kudos: 625
My Contributions


When you load an image into a movieclip, it tends to clobber anything that was in the movieclip previously. I'm not sure if that's what's causing your problem, but is it possible that the placeholder has some of the code you're trying to run on it?

My ActionScript is a bit rusty, but another idea is that it might be having trouble finding the function - try it with an anonymous function:
CODE

var xml = new XML();
xml.ignoreWhite();
xml.onLoad = function() {
    var nodes = this.firstChild.childNodes;
    totalItems = nodes[1].childNodes.length-1;
    for (var i = 1; i<totalItems; i += 2) {
        var mc = main_mc.attachMovie("icon", "item_"+i, 100+i);
        mc.loadMovie(nodes[1].childNodes[i].attributes.url);
        mc.id = i;
        mc.onMouseMove = function() {
                    id = this.id;
                    mx = _xmouse;
                    my = _ymouse;
                    this._x = mx+(this._width*id);
                    this._y = my+(this._height*id);
        }
    }

}
xml.load(xml_path);
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 23 Jun, 2008 - 03:06 PM
Post #3


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,223



Thanked 38 times

Dream Kudos: 725
My Contributions


I see where you are comming from, but it doesn't seem to work any differently between using an annonymous function and using the one I am pointing at.

If I use the _parent.onPress method it works fine, but that isn't what I am wanting to use in the long run...

Also, if I get rid of the load image portion of the code it works fine:

CODE
var xml = new XML();
xml.ignoreWhite();
xml.onLoad = function() {
    var nodes = this.firstChild.childNodes;
    totalItems = nodes[1].childNodes.length-1;
    for (var i = 1; i<totalItems; i += 2) {
        var mc = main_mc.attachMovie("icon", "item_"+i, 100+i);
       // mc.loadMovie(nodes[1].childNodes[i].attributes.url);
        mc.id = i;
        mc.onMouseMove = follow_mouse;
    }

}
xml.load(xml_path);




This post has been edited by BetaWar: 23 Jun, 2008 - 03:08 PM
User is online!Profile CardPM

Go to the top of the page

BetaWar
post 23 Jun, 2008 - 03:58 PM
Post #4


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,223



Thanked 38 times

Dream Kudos: 725
My Contributions


Okay, I got it to work. What you have to do is create a new movieclip inside the mc movieclip and thengive it an instance name (mine was "main") then attach the image the mc.main instead of mc. At this point when you tell mc.onMouseMove to be a function it works. Don't ask me why, but it works.

Final Code:
CODE
var xml = new XML();
xml.ignoreWhite();
xml.onLoad = function() {
    var nodes = this.firstChild.childNodes;
    totalItems = nodes[1].childNodes.length-1;
    for (var i = 1; i<totalItems; i += 2) {
        var mc = main_mc.attachMovie("icon", "item_"+i, 100+i);
        mc.main.loadMovie(nodes[1].childNodes[i].attributes.url);
        mc.id = i;
        mc.onMouseMove = follow_mouse;
    }

}
xml.load(xml_path);


Now, I have a nother question (which sadly enough doesn't go along with the topic title). How do you create new blank movieclips with Actionsctript and give them an instance name?
User is online!Profile CardPM

Go to the top of the page

girasquid
post 23 Jun, 2008 - 04:44 PM
Post #5


Barbarbar

Group Icon
Joined: 3 Oct, 2006
Posts: 1,156



Thanked 6 times

Dream Kudos: 625
My Contributions


Take a look at createEmptyMovieClip().
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 23 Jun, 2008 - 11:36 PM
Post #6


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,223



Thanked 38 times

Dream Kudos: 725
My Contributions


Thanks, I too came to that conclusion (with a lot of looking through every function AS 2 offers).
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/5/08 05:39PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month