作者STARGAZERs (星之影)
看板Flash
标题[问题] 关於 Sprite 与记忆体清除的问题
时间Tue Mar 30 11:48:14 2010
想请问一下版上个位高手一些关於 Function 中物件的问题
还有 Sprite 的问题
我现在在练习写游戏!!
当中会有一个商店 ~
假设商店当中有十个商品
(因为商品当中有图与资料所以我用Sprite包装)
但是画面中只能显示两个
所以我是写了一个 Function ListShow
去创造商品资讯并且传回到预设的 Sprite
同时这 function 还要负责刷新商品资讯
(当使用者呼叫下一页的资讯的时候)
程式的片段在下面
我的问题是 在 ListShow 关於 renew 的程式码似乎没有作用?
如果我用 Container1 = ListShow(Container1, SpriteIndex, 0);
由於资料会写回来原本的容器 画面上的东西会更新
如果我用 ListShow(Container1, SpriteIndex, 1);
Container1 的话面不会消失 所以表示我 renew 的部份没效?
那我到底要怎麽写 才可以将 Container1 从画面中清除 同时从记忆体中清除
还请版上先进指导 ~
var Container1
function CallShop(me:MouseEvent) {
// 创造商品资讯的 Function ListShow
Container1 = ListShow(Container1, SpriteIndex, 0);
}
function ListShow(Bcontainer:Sprite, Sindex:int, renew:int = 0) {
if (renew == 1) {
Bcontainer.removeEventListener(MouseEvent.CLICK, FunctionX);
stage.removeChild(Bcontainer);
Bcontainer = null;
}
Bcontainer = new Sprite;
Bcontainer.addEventListener(MouseEvent.CLICK, FunctionX);
stage.addChild(Bcontainer);
stage.setChildIndex(RL_btn, stage.numChildren -1);
//trace(Bcontainer.name);
return Bcontainer;
}
--
要看着前方前进
要是往旁边或是往下方看的话
便会忌妒... 害怕...
看着前方的人
注视着群星的人...
STARGAZER 观星者...
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.43.122.216
1F:推 cjcat2266:Flash Player使用garbage collector记忆体 03/30 12:05
2F:→ cjcat2266:一般是无法直接"命令"garbage collector清除记忆体的 03/30 12:05
3F:→ cjcat2266:当有需要记忆体,GC自然会去把没用的物件清除掉 03/30 12:06
4F:→ STARGAZERs:所以说他一切都自动@@" 03/30 12:25
5F:→ STARGAZERs:那我在 renew 那段程式码当中做 removeChild 没效 03/30 12:30
6F:→ STARGAZERs:又是甚麽原因阿@@" 03/30 12:30
7F:推 cjcat2266:你要确定BContainer是先前addChild()在stage之下才行 03/30 13:17
8F:→ STARGAZERs:我还挺确定的 因为 addChild 是在倒数第三行... 03/30 13:59
9F:→ STARGAZERs:说错 是倒数第四行 我是希望把显示 删除 都放在 03/30 14:00
10F:→ STARGAZERs:同一个 function 当中@@“ 03/30 14:00
11F:推 cjcat2266:那你怎麽先removeChild()才addChild()呢? 03/30 14:42
12F:→ aquarianboy:当你的renew == 1的时候,会先做removeChild的动作 03/30 14:44
13F:→ aquarianboy:这时候不会发生错误讯息吗? 03/30 14:44
用修文来回答 ~
我做 removeChild 不会有错误讯息!!
我有想过是不是 Function 内的 Bcontainer:Sprite
只是继承了外部 conatiner1 的一切的副本
可是我可以取消监听 却不能移除他 这点好奇怪喔...
※ 编辑: STARGAZERs 来自: 114.43.122.216 (03/30 16:23)
14F:推 emily114005:renew==1时,removeChild做完又会再做一次addChild喔 03/30 16:22
15F:→ STARGAZERs:是没错阿 可是重新 addchild 前我有把他变成空的阿 03/30 16:23
16F:→ STARGAZERs:removeChild 後面的 = null 03/30 16:26
17F:→ STARGAZERs:或是 Bcontainer = new sprite; 03/30 16:26
18F:→ STARGAZERs:Orz 我单独把这段 renew 拉出来就不能用了 03/30 17:06
19F:→ STARGAZERs:会 Error... 请问哪位高手能提供一下正确的写法吗 03/30 17:08
20F:→ STARGAZERs:找到问题点了... removeChild 的写法是... 03/30 17:10
21F:→ STARGAZERs:removeChild(); 而不是 stage.removeChild(); 03/30 17:10