如何在拖拽中加载外部图像使用 AS3 删除对象?

发布于 2024-10-20 02:08:44 字数 4656 浏览 1 评论 0原文

新消息 - 我最终应用的解决方案

这是我用来将外部图像加载到影片剪辑中并进行拖放的方法。 反馈部分与旧代码几乎相同。

var holdermc_Arr:Array = new Array(4);
for(var i:uint = 0; i < holdermc_Arr.length; i++)
{
    holdermc_Arr[i] = this["panel" + (i+1) + "_mc"];

    var myLoader:Loader = new Loader();
    var fileRequest:URLRequest = new URLRequest("images/" + (i+1) + ".jpg");
    myLoader.load(fileRequest);

    holdermc_Arr[i].addChild(myLoader);

    holdermc_Arr[i].addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    holdermc_Arr[i].addEventListener(MouseEvent.MOUSE_UP, dropIt);
    holdermc_Arr[i].buttonMode = true;
}

var startX:Number;
var startY:Number;
var counter:Number = 0;

function pickUp(event:MouseEvent):void {
startX = event.currentTarget.x;
startY = event.currentTarget.y;

setChildIndex(MovieClip(event.currentTarget),numChildren-1);
event.currentTarget.startDrag();
reply_txt.text = "";

}

旧消息如下

这是我对当前错误的最新尝试。

我实际上想使用我之前尝试的代码,因为对我来说,添加多个可拖动的影片剪辑更容易。

但无论使用哪种代码,根本问题似乎是我没有为“Loader”正确设置属性。

如果需要示例链接,请告诉我。

错误如下:

ReferenceError:错误 #1069:找不到属性 dropTarget flash.display.Loader 并没有默认值。在 cs5test_fla::MainTimeline/ReleaseToDrop()

代码如下

var myLoader:Loader = new Loader(); 
panel1_mc.addChild(myLoader); 
var url:URLRequest = new URLRequest("uploadedImages/photo_1.jpeg"); 
myLoader.load(url);

var startX:Number;
var startY:Number;
var counter:Number = 0;

panel1_mc.addEventListener(MouseEvent.MOUSE_DOWN, ClickToDrag);

function ClickToDrag(event:MouseEvent):void
{
panel1_mc.startDrag();
startX = event.target.x;
startY = event.target.y;
    reply_txt.text = "";
event.target.parent.addChild(event.target);
}

stage.addEventListener(MouseEvent.MOUSE_UP, ReleaseToDrop);

function ReleaseToDrop(event:MouseEvent):void
{
panel1_mc.stopDrag();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);

if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){

    reply_txt.text = "Good Job!";
    event.target.x = myTarget.x;
    event.target.y = myTarget.y;
    event.target.removeEventListener(MouseEvent.MOUSE_DOWN, ClickToDrag);
    event.target.removeEventListener(MouseEvent.MOUSE_UP, ReleaseToDrop);
    event.target.buttonMode = false;
    counter++;
} else {
    reply_txt.text = "Try Again!";
    event.target.x = startX;
    event.target.y = startY;
}
if(counter == 1){
    reply_txt.text = "Congrats, you're finished!";
}
}

旧消息如下

我仍在挣扎。

我对这一切都很陌生,所以我真的正在尽我所能来掌握它。

我尝试使用的 ActionScript 一直位于底部。

如果有人尝试查看此代码并告诉我如何将外部图像加载到每个可拖动的影片剪辑而不会出现以下错误,我将非常感激。

ReferenceError:错误 #1069:找不到属性 startDrag flash.display.Loader 并没有默认值。在 DragdropDilema_fla::MainTimeline/pickUp() ReferenceError:错误#1069: 在 flash.display.Loader 上找不到属性 stopDrag 并且没有 默认值。在dragdropDilema_fla::MainTimeline/dropIt()

我尝试使用一个简单的 var 加载器,但对于我在错误中看到的内容,我必须设置 startDrag 属性来处理加载的图像,而不是使用可拖动的影片剪辑目前到位??

,我会很简单

var myLoader:Loader = new Loader(); 
imageHolder.addChild(myLoader); 
var url:URLRequest = new URLRequest("uploadedImages/photo_1.jpeg"); 
myLoader.load(url);

我以为如果我对每个使用以下内容,然后执行类似的操作

square_mc.imageHolder.addChild(myLoader);

:但是当我这样做时,当我单击图像时,无论如何我都会收到错误。

这是完整的代码:它应该计算对象何时达到目标,并在整个过程中和最后给出一条消息。

panel1_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
panel1_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);

panel1_mc.buttonMode = true;


var startX:Number;
var startY:Number;
var counter:Number = 0;

function pickUp(event:MouseEvent):void {
startX = event.target.x;
startY = event.target.y;
event.target.startDrag(true);
reply_txt.text = "";
event.target.parent.addChild(event.target);
}

function dropIt(event:MouseEvent):void {
event.target.stopDrag();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
    reply_txt.text = "Good Job!";
    event.target.x = myTarget.x;
    event.target.y = myTarget.y;
    event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
    event.target.buttonMode = false;
    counter++;
} else {
    reply_txt.text = "Try Again!";
    event.target.x = startX;
    event.target.y = startY;
}
if(counter == 1){
    reply_txt.text = "Congrats, you're finished!";
}
}

谢谢。

NEW MESSAGE - THE SOLUTION I ENDED UP APPLYING

This is what I used to load an external image into a movie clip and drag and drop.
The feedback part is pretty much the same from the old code.

var holdermc_Arr:Array = new Array(4);
for(var i:uint = 0; i < holdermc_Arr.length; i++)
{
    holdermc_Arr[i] = this["panel" + (i+1) + "_mc"];

    var myLoader:Loader = new Loader();
    var fileRequest:URLRequest = new URLRequest("images/" + (i+1) + ".jpg");
    myLoader.load(fileRequest);

    holdermc_Arr[i].addChild(myLoader);

    holdermc_Arr[i].addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    holdermc_Arr[i].addEventListener(MouseEvent.MOUSE_UP, dropIt);
    holdermc_Arr[i].buttonMode = true;
}

var startX:Number;
var startY:Number;
var counter:Number = 0;

function pickUp(event:MouseEvent):void {
startX = event.currentTarget.x;
startY = event.currentTarget.y;

setChildIndex(MovieClip(event.currentTarget),numChildren-1);
event.currentTarget.startDrag();
reply_txt.text = "";

}

OLD MESSAGE BELLOW

This is my latest attempt with the current error I am getting.

I actually would like to work with the code of my previous attempt because to me is easier that way to add multiple draggable movie clips.

But whichever the code, what seems to be the fundamental problem is that I am not setting up the property correctly for the "Loader".

Let me know if a link to the example is needed.

The error is the following:

ReferenceError: Error #1069: Property dropTarget not found on
flash.display.Loader and there is no default value. at
cs5test_fla::MainTimeline/ReleaseToDrop()

The code is the following

var myLoader:Loader = new Loader(); 
panel1_mc.addChild(myLoader); 
var url:URLRequest = new URLRequest("uploadedImages/photo_1.jpeg"); 
myLoader.load(url);

var startX:Number;
var startY:Number;
var counter:Number = 0;

panel1_mc.addEventListener(MouseEvent.MOUSE_DOWN, ClickToDrag);

function ClickToDrag(event:MouseEvent):void
{
panel1_mc.startDrag();
startX = event.target.x;
startY = event.target.y;
    reply_txt.text = "";
event.target.parent.addChild(event.target);
}

stage.addEventListener(MouseEvent.MOUSE_UP, ReleaseToDrop);

function ReleaseToDrop(event:MouseEvent):void
{
panel1_mc.stopDrag();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);

if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){

    reply_txt.text = "Good Job!";
    event.target.x = myTarget.x;
    event.target.y = myTarget.y;
    event.target.removeEventListener(MouseEvent.MOUSE_DOWN, ClickToDrag);
    event.target.removeEventListener(MouseEvent.MOUSE_UP, ReleaseToDrop);
    event.target.buttonMode = false;
    counter++;
} else {
    reply_txt.text = "Try Again!";
    event.target.x = startX;
    event.target.y = startY;
}
if(counter == 1){
    reply_txt.text = "Congrats, you're finished!";
}
}

OLDER MESSAGE BELLOW

I am still struggling.

I am new to all this so I am really tying the best I can to grasp it.

The ActionScript I am trying to work with is all the way at the bottom.

I'd really appreciate if someone tries to look at this code and tell me how can I load external images to each draggable movie clip with out getting the following error.

ReferenceError: Error #1069: Property startDrag not found on
flash.display.Loader and there is no default value. at
dragdropDilema_fla::MainTimeline/pickUp() ReferenceError: Error #1069:
Property stopDrag not found on flash.display.Loader and there is no
default value. at dragdropDilema_fla::MainTimeline/dropIt()

I tried to use a simple var loader but for what I can see on the error, I have to set up the startDrag property to work with the Loaded image and not with the draggable movie clips that are currently in place??

I thought I would have just been simple if I had use the following for each:

var myLoader:Loader = new Loader(); 
imageHolder.addChild(myLoader); 
var url:URLRequest = new URLRequest("uploadedImages/photo_1.jpeg"); 
myLoader.load(url);

and then do something like:

square_mc.imageHolder.addChild(myLoader);

but when I do that I get the error anyway when I click on the image.

Here is the entire code: It is supposed to count when the object meet their target and gives a message all through out and at the end.

panel1_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
panel1_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);

panel1_mc.buttonMode = true;


var startX:Number;
var startY:Number;
var counter:Number = 0;

function pickUp(event:MouseEvent):void {
startX = event.target.x;
startY = event.target.y;
event.target.startDrag(true);
reply_txt.text = "";
event.target.parent.addChild(event.target);
}

function dropIt(event:MouseEvent):void {
event.target.stopDrag();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
    reply_txt.text = "Good Job!";
    event.target.x = myTarget.x;
    event.target.y = myTarget.y;
    event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
    event.target.buttonMode = false;
    counter++;
} else {
    reply_txt.text = "Try Again!";
    event.target.x = startX;
    event.target.y = startY;
}
if(counter == 1){
    reply_txt.text = "Congrats, you're finished!";
}
}

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔少女心 2024-10-27 02:08:44
import flash.display.DisplayObjectContainer;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;  
import flash.events.MouseEvent;
import flash.net.URLRequest;

var _dragMc:MovieClip = new MovieClip();
addChild(_dragMc);

loadImage("image.jpg")

function loadImage(path:String):void
{
    var _loader:Loader = new Loader();
    var _loaderToLoad:URLRequest = new URLRequest(path);
    _loader.load(_loaderToLoad);
    _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded, false, 0, true);
}

function imageLoaded(evt:Event):void
{
   addLoader(evt.currentTarget.loader, _dragMc);
}

function addLoader(loader:Loader, container:DisplayObjectContainer):void
{
    container.addChild(loader);
    addListeners();
}

function addListeners():void
{
    _dragMc.addEventListener(MouseEvent.MOUSE_DOWN, setDrag, false, 0, true);
}

function setDrag(evt:MouseEvent):void
{
    _dragMc.addEventListener(MouseEvent.MOUSE_MOVE, doDrag, false, 0, true);
    _dragMc.removeEventListener(MouseEvent.MOUSE_DOWN, setDrag);
}

function doDrag(evt:MouseEvent):void
{
    _dragMc.startDrag(false, null);
    stage.addEventListener(MouseEvent.MOUSE_UP, endDrag, false, 0, true);
    _dragMc.removeEventListener(MouseEvent.MOUSE_MOVE, doDrag);
}

function endDrag(evt:MouseEvent):void
{
    _dragMc.stopDrag();
    _dragMc.addEventListener(MouseEvent.MOUSE_DOWN, setDrag, false, 0, true);
    stage.removeEventListener(MouseEvent.MOUSE_UP, endDrag);
}

这会将图像加载到名为 _dragMcMovieClip 上。剩下的就取决于你了。我还添加了一些快速代码,使您能够拖动 _dragMc

import flash.display.DisplayObjectContainer;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;  
import flash.events.MouseEvent;
import flash.net.URLRequest;

var _dragMc:MovieClip = new MovieClip();
addChild(_dragMc);

loadImage("image.jpg")

function loadImage(path:String):void
{
    var _loader:Loader = new Loader();
    var _loaderToLoad:URLRequest = new URLRequest(path);
    _loader.load(_loaderToLoad);
    _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded, false, 0, true);
}

function imageLoaded(evt:Event):void
{
   addLoader(evt.currentTarget.loader, _dragMc);
}

function addLoader(loader:Loader, container:DisplayObjectContainer):void
{
    container.addChild(loader);
    addListeners();
}

function addListeners():void
{
    _dragMc.addEventListener(MouseEvent.MOUSE_DOWN, setDrag, false, 0, true);
}

function setDrag(evt:MouseEvent):void
{
    _dragMc.addEventListener(MouseEvent.MOUSE_MOVE, doDrag, false, 0, true);
    _dragMc.removeEventListener(MouseEvent.MOUSE_DOWN, setDrag);
}

function doDrag(evt:MouseEvent):void
{
    _dragMc.startDrag(false, null);
    stage.addEventListener(MouseEvent.MOUSE_UP, endDrag, false, 0, true);
    _dragMc.removeEventListener(MouseEvent.MOUSE_MOVE, doDrag);
}

function endDrag(evt:MouseEvent):void
{
    _dragMc.stopDrag();
    _dragMc.addEventListener(MouseEvent.MOUSE_DOWN, setDrag, false, 0, true);
    stage.removeEventListener(MouseEvent.MOUSE_UP, endDrag);
}

This loads an image onto a MovieClip called _dragMc. The rest from there is up to you. I also added some quick code that will enable you to drag _dragMc around.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文