A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Depth help

  1. #1
    Junior Member
    Join Date
    Aug 2008
    Posts
    7

    Depth help

    I have been trying to change the depth of an object. I have only just started learning AS3, so I haven't looked into this yet.

    So far I have been using a trial-&-error method to try to get this piece of code to work which I found on a site, but I have not succeeded:

    Code:
    setChildIndex(example,numChildren-1);
    I am putting this code in it's class, and I tried replacing the word example with 'this'. Can someone please help me? When I test the swf it comes up with this in output:

    Code:
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    	at flash.display::DisplayObjectContainer/setChildIndex()
    	at Foreground/enterFrame()
    If there is any more information I need to give to be more helpful just ask

  2. #2
    Member
    Join Date
    Sep 2010
    Posts
    37
    I'm no expert either, but I'll try to answer to the extent of my knowledge.

    I think you're having problems because your trying to use setchildindex and numchildren on an object itself (the parent trying to place itself as a child inside itself?) so, i dont know what other classes and/or what structure you are using but you could try
    Actionscript Code:
    Movieclip(parent).setChildIndex(this, Movieclip(parent).numChildren-1);
    see if that works. also, you could create a variable like
    Actionscript Code:
    var parentClip = MovieClip(parent);
    and use the parentClip variable instead of MovieClip(parent) all the time.

  3. #3
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    by saying

    PHP Code:
    setChildIndex(thisnumChildren 1); 
    you're trying to have the parent call the method on itself.

    whatever is containing the movieclip to move is the one that should call the method - this is the parent. the movieclip you want to change depths on is called the child. the first argument (where it shows "example") is the one that you want to move up.

    both need references - either variables, or instance names. i'll use "parentInstance" and "childInstance" for the example.

    PHP Code:
    parentInstance.setChildIndex(childInstanceparentInstance.numChildren 1); 
    "this" is a reference to whatever scope you're in. if you're calling it from a class, it's a reference to the active instance of the class. so if the class instance was trying to move a movieclip that it contained, you could say:

    PHP Code:
    this.setChildIndex(childInstancethis.numChildren 1); 
    "this" is inferred, so the above is exactly the same as saying:

    PHP Code:
    setChildIndex(childInstancenumChildren 1); 
    if you want to move "this", instead of having it move a child, you'd need a reference to it's parent.

    PHP Code:
    parent.setChildIndex(thisparent.numChildren 1); 
    hth, gl

  4. #4
    Junior Member
    Join Date
    Aug 2008
    Posts
    7
    Thank you both, I had no trouble solving the problem with this information

  5. #5
    ___________________
    Join Date
    May 2004
    Posts
    3,174

  6. #6
    Member
    Join Date
    Sep 2010
    Posts
    37

  7. #7
    Junior Member
    Join Date
    Aug 2008
    Posts
    7
    Oh, now I have another problem.

    I've tried using this code from a class named EnemyShip:

    Code:
    parent.setChildIndex(stage.Foreground, Foreground.foreIndex);
    I am pretty much just testing for anything that works, but haven't got it to work in the end. I'm simply trying to put my enemies behind the foreground, which is controlled by a class named Foreground.

    foreIndex is a static variable created in the Foreground class which is the Foreground's depth:

    Code:
    foreIndex=getChildIndex(this);
    Any help would be appreciated.
    Last edited by dasmitimsad; 11-12-2010 at 06:53 PM.

  8. #8
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    How did you add Foreground to the stage? Can you supply more code?

    p.s. It's a good practice to always tell system what instance you're trying to call some method on. For example:
    Actionscript Code:
    stage.getChildIndex(this);
    or
    Actionscript Code:
    this.parent.getChildIndex(this);

    instead of simple
    Actionscript Code:
    getChildIndex(this);

    This will help you avoid many problems in future

  9. #9
    Junior Member
    Join Date
    Aug 2008
    Posts
    7
    Okay, well Foreground is just an object on the stage, but I was planning on adding it to the stage by code after I got some other code sorted.

    And no problem, I'll try and do that from now on.

  10. #10
    Junior Member
    Join Date
    Aug 2008
    Posts
    7
    I've changed the code slightly since my posts above, so keep that in mind. I've uploaded the code from my EnemyShip class and my Foreground class. The compiler error is shown below the links.

    EnemyShip
    Foreground

    Sorry for what it looks like making you figure out a lot.

    The compiler error which I receive is:
    PHP Code:
    LocationForeground.as, Line 18
    Description
    1119Access of possibly undefined property EnemyShip through a reference with static type Class.
    Sourceparent.setChildIndex(Game.EnemyShipforeIndex); 
    Last edited by dasmitimsad; 11-12-2010 at 07:04 PM.

  11. #11
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    you should back up a bit. from your past about 4 posts back, there are several problems already... you can't call getChildIndex like that... the parent needs to call it on a child, so this won't work:

    PHP Code:
    foreIndex=getChildIndex(this); 
    next, stage is never going to have a property named "Foreground", so this too won't work:

    PHP Code:
    parent.setChildIndex(stage.Foreground); 
    also, you're not at all sure that the calling scope is going to have the same scope as the target - so you can't just call parent.getChildIndex(somethingElseEntirely) - the target of the call (somethingElseEntirely) will only give back it's index if getChildIndex is invoked by it's parent. you might say something like this:

    PHP Code:
    somethingElseEntirely.parent.getChildIndex(somethingElseEntirely); 
    moving on... it looks like you're confusing classes and class instances. if you have a class called Enemy, it's instances aren't targetted like that. it looks like you have a class called Foreground - that doesn't mean you can access the instance of that class by calling Foreground.whatever - using syntax like that, Flash would look at the class itself and look for a "static" member - a special value assigned directly to a class - not an instance of a class.

    this is a pretty involved topic that you really need to master before proceeding, and a discussion probably well beyond the scope of this board.

    experiment with classes and class instances on very low-level stuff first - make a class, instance it a couple times, then just trace it out to see if you can find it - then trace out it's properties, etc... once you've got that mastered, experiment with static members for a while.

    hth

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center