Event.ADDED和Event.ADDED_TO_STAGE的区别

本文详细解释了ActionScript 3中Event.ADDED与Event.ADDED_TO_STAGE两种事件的区别及应用场景。前者指对象或其子对象被添加到显示列表中,后者特指对象本身被添加到可显示的列表从而能访问到舞台。

引用 http://www.actionscript.org/forums/showthread.php3?t=220154


var _container:Sprite = new Sprite();
 
//_container.addEventListener(Event.ADDED,onAdded);
//_container.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
 
function onAdded(e:Event):void {
    trace("onAdded:",e);
}
 
function onAddedToStage(e:Event):void {
    trace("onAddedToStage:",e);
}
 
 
addChild(_container);
var _sub:Sprite = new Sprite();
_sub.addEventListener(Event.ADDED,onAdded);
_sub.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
 
_container.addChild(_sub);

Basically added is for an object or any of its children being added to any display list, and added to stage is for an object (not including its children) being added to the active display list giving it access to stage.

added对象自身或者其子对象被添加到任意的显示列表中[相当于有对象加到它其中或者它被加到其他对象中](比如container.addChild(sub),但是container不一定已经添加到舞台,即此时还是不可见的)【只要被addChild】

addedToStage表示对象自身(不包括子对象)被添加到可显示的显示列表,这样对象就可以访问到舞台。【即对象自身可以访问到舞台】

所以上例中是否先addChild(container)结果也很明显


var all:Sprite = new Sprite;
			
			var container:Sprite = new Sprite;
			
			var sub:Sprite = new Sprite;
			
			container.addEventListener(Event.ADDED,onAdded);
			container.addChild(sub);
			all.addChild(container);
// 触发两次


package; import objects.screen.Graphics; import objects.screen.FPS; import flixel.graphics.FlxGraphic; import flixel.FlxGame; import flixel.FlxState; import haxe.io.Path; import openfl.Assets; import openfl.system.System; import openfl.Lib; import openfl.display.Sprite; import openfl.events.Event; import openfl.display.StageScaleMode; import openfl.events.KeyboardEvent; import lime.system.System as LimeSystem; import lime.app.Application; import states.TitleState; import mobile.backend.Data; #if desktop import backend.ALSoftConfig; #end #if hl import hl.Api; #end #if linux import lime.graphics.Image; @:cppInclude('./external/gamemode_client.h') @:cppFileCode(' #define GAMEMODE_AUTO ') #end class Main extends Sprite { var game = { width: 1280, // WINDOW width height: 720, // WINDOW height initialState: TitleState, // initial game state zoom: -1.0, // game state bounds framerate: 60, // default framerate skipSplash: true, // if the default flixel splash screen should be skipped startFullscreen: false // if the game should start at fullscreen mode }; public static var fpsVar:FPS; public static var watermark:Watermark; #if mobile public static final platform:String = "Phones"; #else public static final platform:String = "PCs"; #end // You can pretty much ignore everything from here on - your code should go in your states. public static function main():Void { Lib.current.addChild(new Main()); #if cpp cpp.NativeGc.enable(true); cpp.NativeGc.run(true); #end } public function new() { super(); #if android SUtil.doPermissionsShit(); #end mobile.backend.CrashHandler.init(); #if windows @:functionCode(" #include <windows.h> #include <winuser.h> setProcessDPIAware() // allows for more crisp visuals DisableProcessWindowsGhosting() // lets you move the window and such if it's not responding ") #end if (stage != null) { init(); } else { addEventListener(Event.ADDED_TO_STAGE, init); } #if VIDEOS_ALLOWED hxvlc.util.Handle.init(#if (hxvlc >= "1.8.0") ['--no-lua'] #end); #end } private function init(?E:Event):Void { if (hasEventListener(Event.ADDED_TO_STAGE)) { removeEventListener(Event.ADDED_TO_STAGE, init); } setupGame(); } private function setupGame():Void { var stageWidth:Int = Lib.current.stage.stageWidth; var stageHeight:Int = Lib.current.stage.stageHeight; if (game.zoom == -1.0) { var ratioX:Float = stageWidth / game.width; var ratioY:Float = stageHeight / game.height; game.zoom = Math.min(ratioX, ratioY); game.width = Math.ceil(stageWidth / game.zoom); game.height = Math.ceil(stageHeight / game.zoom); } #if LUA_ALLOWED llua.Lua.set_callbacks_function(cpp.Callable.fromStaticFunction(psychlua.CallbackHandler.call)); #end Controls.instance = new Controls(); ClientPrefs.loadDefaultKeys(); #if mobile #if android if (!FileSystem.exists(AndroidEnvironment.getExternalStorageDirectory() + '/.' + Application.current.meta.get('file'))) FileSystem.createDirectory(AndroidEnvironment.getExternalStorageDirectory() + '/.' + Application.current.meta.get('file')); #end Sys.setCwd(SUtil.getStorageDirectory()); #end #if ACHIEVEMENTS_ALLOWED Achievements.load(); #end addChild(new FlxGame(#if (openfl >= "9.2.0") 1280, 720 #else game.width, game.height #end, game.initialState, #if (flixel < "5.0.0") game.zoom, #end game.framerate, game.framerate, game.skipSplash, game.startFullscreen)); Achievements.load(); fpsVar = new FPS(5, 5); addChild(fpsVar); if(fpsVar != null) { fpsVar.scaleX = fpsVar.scaleY = ClientPrefs.data.FPSScale; fpsVar.visible = ClientPrefs.data.showFPS; } Lib.current.stage.align = "tl"; Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE; switch (ClientPrefs.data.gameQuality) { case 0: FlxG.game.stage.quality = openfl.display.StageQuality.LOW; case 1: FlxG.game.stage.quality = openfl.display.StageQuality.HIGH; case 2: FlxG.game.stage.quality = openfl.display.StageQuality.MEDIUM; case 3: FlxG.game.stage.quality = openfl.display.StageQuality.BEST; } #if mobile FlxG.fullscreen = true; #end var image:String = Paths.modFolders('images/menuExtend/Others/watermark.png'); if (FileSystem.exists(image)) { if(watermark != null) removeChild(watermark); watermark = new Watermark( 5, Lib.current.stage.stageHeight - 5, 0.4); addChild(watermark); watermark.y -= watermark.bitmapData.height; } if(watermark != null) { watermark.scaleX = watermark.scaleY = ClientPrefs.data.WatermarkScale; watermark.y += (1 - ClientPrefs.data.WatermarkScale) * watermark.bitmapData.height; watermark.visible = ClientPrefs.data.showWatermark; } #if linux var icon = Image.fromFile("icon.png"); Lib.current.stage.window.setIcon(icon); #end #if desktop FlxG.stage.addEventListener(KeyboardEvent.KEY_UP, toggleFullScreen); #end #if android FlxG.android.preventDefaultKeys = [BACK]; #end #if mobile LimeSystem.allowScreenTimeout = ClientPrefs.data.screensaver; //Application.current.addEventListener(Event.ACTIVATE, onActivate); //Application.current.addEventListener(Event.DEACTIVATE, onDeactivate); #end #if html5 FlxG.autoPause = false; FlxG.mouse.visible = false; #end #if DISCORD_ALLOWED DiscordClient.prepare(); #end #if mobile LimeSystem.allowScreenTimeout = ClientPrefs.data.screensaver; #end Data.setup(); if (ClientPrefs.data.gcFreeZone) cpp.NativeGc.enterGCFreeZone; //GcZoneChange(); // shader coords fix FlxG.signals.gameResized.add(function (w, h) { //if(fpsVar != null) //fpsVar.positionFPS(10, 3, Math.min(Lib.current.stage.stageWidth / FlxG.width, Lib.current.stage.stageHeight / FlxG.height)); if (FlxG.cameras != null) { for (cam in FlxG.cameras.list) { if (cam != null && cam.filters != null) resetSpriteCache(cam.flashSprite); } } if (FlxG.game != null) resetSpriteCache(FlxG.game); }); } static function resetSpriteCache(sprite:Sprite):Void { @:privateAccess { sprite.__cacheBitmap = null; sprite.__cacheBitmapData = null; } } function toggleFullScreen(event:KeyboardEvent){ if(Controls.instance.justReleased('fullscreen')) FlxG.fullscreen = !FlxG.fullscreen; } function onDeactivate(e:Event) { //暂时没需要 } function onActivate(e:Event) { // 延迟设置确保系统状态稳定 haxe.Timer.delay(() -> { //Application.current.window.displayMode.refreshRate = ClientPrefs.data.framerate; }, 50); // 50ms延迟适配慢速设备 } static public var type:Bool = ClientPrefs.data.gcFreeZone; static public function GcZoneChange() { if (type == true) { cpp.NativeGc.exitGCFreeZone; type = false; } else { cpp.NativeGc.enterGCFreeZone; type = true; } } } 添加锁定fps
最新发布
08-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值