I try to drag something, and on stopdrag it should start a function if certain requirements are fulfilled:
if(e.target.dropTarget.parent == targetName || e.target.dropTarget.parent.parent.parent.getChildByName("cloud").getChildByName("itemPlacer").getChildAt(1) == targetName){
This works well when I drag and stopdrag correctly (requirements are fullfilled) but if the requirements aren't met I get an error saying that the place I refer to cannot be null. I know that the problem lies after the ||
part, but how can I resolve this?
Is there a shorter way of doing these checks? At the moment, I am going through a lot of parents and getchildat statements and was wondering if there was another way.
You can assign a variable to your container then use the variable in your conditional statements.
Edit:
In order to access the getChildByName method, your elements should be cast as DisplayObjectContainer at the minimum. MovieClip , Sprite extend DisplayObjectContainer, so you can use either. Here I use MovieClip but the choice was arbitrary.
var main:MovieClip = e.target.dropTarget.parent ; var container:MovieClip = main.parent.parent as MovieClip; var cloud:MovieClip = container.getChildByName("cloud"); var itemPlacer:MovieClip = cloud.getChildByName("itemPlacer"); var child:MovieClip = container.getChildAt(1); if( main == targetName || child == targetName ) //your code here
For the other question, check for null values before anything else.
if(main != null && child != null) { if( main == targetName || child == targetName ) { //your code here } }else{ //your code here }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.