public virtual DisplayObjectX addChildAt(DisplayObjectX child, int index) { int numChildren = mChildren.Count; if (index >= 0 && index <= numChildren) { if (child.parent == this) { setChildIndex(child, index); // avoids dispatching events } else { child.removeFromParent(); // 'splice' creates a temporary object, so we avoid it if it's not necessary if (index == numChildren) { mChildren.Add(child); } else { mChildren.Insert(index, child); } child.setParent(this); //child.dispatchEventWith(EventX.ADDED, true); if (stage != null) { DisplayObjectContainerX container = child as DisplayObjectContainerX; if (container != null) { //递归上去; //container.simpleDispatch(EventX.ADDED_TO_STAGE); } else { child.simpleDispatch(EventX.ADDED_TO_STAGE); } } } return(child); } else { throw new RankException(); } }
internal void setParent(DisplayObjectContainerX value) { DisplayObjectX ancestor = value; while (ancestor != this && ancestor != null) { ancestor = ancestor.mParent; } if (ancestor == this) { throw new ArgumentException( "An object cannot be added as a child to itself or one of its children (or children's children, etc.)"); } else { mParent = value; } }
public Button(DisplayObjectX upState, string text = "", DisplayObjectX downState = null, TextFormat format = null) { //if (upState == null) throw new ErrorEvent("Texture cannot be null"); mParent = upState.parent; mUpState = upState; mDownState = downState != null ? downState : upState; mBackground = upState; mTextFormat = format; mScaleWhenDown = 0.9f; mAlphaWhenDisabled = 0.5f; mEnabled = true; mIsDown = false; mUseHandCursor = true; mTextBounds = new RectangleX(0, 0, upState.width, upState.height); mContents = new DisplayObjectContainerX(); mContents.addChild(mBackground); addChild(mContents); //addEventListener(TouchEvent.TOUCH, onTouch); addEventListener(MouseEventX.MOUSE_DOWN, onMouseDown); if (text.Length > 0) { this.text = text; } this.x = upState.x; this.y = upState.y; upState.x = upState.y = 0; if (mParent != null) { mParent.addChild(this); } }
public DisplayObjectX removeChildAt(int index, bool dispose = false) { if (index >= 0 && index < numChildren) { DisplayObjectX child = mChildren[index]; child.simpleDispatch(EventX.REMOVED); if (stage != null) { DisplayObjectContainerX container = child as DisplayObjectContainerX; if (container != null) { container.broadcastEventWith(EventX.REMOVED_FROM_STAGE); } else { child.simpleDispatch(EventX.REMOVED_FROM_STAGE); } } child.setParent(null); index = mChildren.IndexOf(child); if (index >= 0) { mChildren.RemoveAt(index); } if (dispose) { child.dispose(); } return(child); } else { throw new RankException("Invalid child index"); } }