Actionscript 3,Flash CC:将数据库中的对象放置在舞台上的库中

时间:2013-12-13 19:26:09

标签: arrays actionscript-3 flash movieclip

你好编程stackoverflow的大师,我希望至少有一个人能够帮助我解决我的编码问题。这是我第一次在这个网站上发帖,所以如果我错过了我的帖子的结构,或者其他任何事情,请告诉我(最好不要在一个居高临下的事情中),我很乐意改变它。

我实际上遇到了一个不同的问题,但我最近意识到我的图书馆里的一些物品没有出现在我的舞台上。希望如果这个问题得到解决,我将不会遇到其他问题。

我正在使用Flash CC和Actionscript 3创建学习模块应用程序,我觉得我对Flash非常熟练,但是现在我的所有代码都在时间轴上,因为当我开始时我不知道包建立。当我完成学习模块后,我会尝试将所有内容移到AS包中,所以请耐心等待。

该模块的当前帧是拖放游戏,其中用户将对于他们在前一帧中选择的动物的正确食物拖动到中间的动物。动物被动态地放置在舞台上,以及一系列六种可能的食物选择,所有的MovieClip都从图书馆中取出。食物元素的阵列实际上不是我遇到的问题,它们出现在我的舞台上毫无问题。我遇到的问题是当用户将正确的食物拖到动物身上并且满足获胜条件时,气球元素阵列不会出现在舞台上。我发现它很奇怪,因为我在食物和气球阵列上使用几乎相同的代码。

这是我的完整代码:

import flash.display.MovieClip;
import flash.events.MouseEvent;

foodPet();

function foodPet():void {
    //all of my pet, food, and balloon library objects have been exported for AS
    var theBird:pet_bird = new pet_bird;
    var theCat:pet_cat = new pet_cat;
    var theChicken:pet_chicken = new pet_chicken;
    var theDog:pet_dog = new pet_dog;
    var theDuck:pet_duck = new pet_duck;
    var theGuinea:pet_guinea = new pet_guinea;
    var theHamster:pet_hamster = new pet_hamster;

    var birdSeed:food_bird_seed = new food_bird_seed;
    var catFood:food_cat_food = new food_cat_food;
    var chickenFeed:food_chicken_feed = new food_chicken_feed;
    var chocolate:food_chocolate = new food_chocolate;
    var dogFood:food_dog_food = new food_dog_food;
    var duckFood:food_duck_food = new food_duck_food;

    var animalList:Array = [theBird, theCat, theChicken, theDog,
        theDuck, theGuinea, theHamster];
    var food1Array:Array = [birdSeed, catFood, chickenFeed,
        chocolate, dogFood, duckFood, 4];
    var xPosFood:Array = new Array();
    var yPosFood:Array = new Array();
    xPosFood = [32, 71, 146, 363, 431, 512];
    yPosFood = [304, 222, 123, 123, 222, 304];
    var animalClip:MovieClip;
    animalClip = animalList[chosenAnimal];
    addChild(animalClip);
    animalClip.x = 256;
    animalClip.y = 287;
    animalClip.name = "selectedAnimal";

    for (var i:uint = 0; i < food1Array.length - 1; i++){ //Where the food gets added
        var isItRight:Boolean = false;
        var foodName:String = ("food" + i);
        var foodClip:MovieClip;
        foodClip = food1Array[i];
        foodClip.x = xPosFood[i];
        foodClip.y = yPosFood[i];
        foodClip.name = foodName;
        addChild(foodClip);
        trace(foodClip.parent);
        foodDragSetup(foodClip, animalClip, food1Array[food1Array.length - 1], isItRight);
    }
}

function foodDragSetup(clip:MovieClip, targ:MovieClip, correctNum:uint, isItRight:Boolean) {
    var beingDragged:Boolean = false;
    var xPos:Number = clip.x;
    var yPos:Number = clip.y;
    clip.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);

    function beginDrag(event:MouseEvent):void
    {
        clip.startDrag();
        if (int(clip.name.substr(4)) == correctNum){
            isItRight = true;
        }
        this.beingDragged = true;
        setChildIndex(clip, numChildren - 1);
        clip.addEventListener(MouseEvent.MOUSE_UP, endDrag);
    }

    function endDrag(event:MouseEvent):void
    {
        if (this.beingDragged) {
            this.beingDragged = false;
            clip.stopDrag();
            if ((isItRight) && (clip.hitTestPoint(targ.x, targ.y, true))){
                trace(targ.name + " has been hit.");
                clip.x = targ.x;
                clip.y = targ.y;
                win_animal_food();
            } else {
                isItRight = false;
                clip.x = xPos;
                clip.y = yPos;
            }
        }
    }
}

function win_animal_food():void {
    const BALLOON_ROW:int = 4;
    var count:uint = 0;
    var altX:uint = 0;

    var bBalloon:blue_balloon = new blue_balloon;
    var gBalloon:green_balloon = new green_balloon;
    var oBalloon:orange_balloon = new orange_balloon;
    var pBalloon:purple_balloon = new purple_balloon;
    var rBalloon:red_balloon = new red_balloon;
    var yBalloon:yellow_balloon = new yellow_balloon;

    var balloonList:Array = [bBalloon, gBalloon, oBalloon,
        pBalloon, rBalloon, yBalloon, bBalloon, gBalloon,
        oBalloon, pBalloon, rBalloon, yBalloon, bBalloon,
        gBalloon, oBalloon, pBalloon];
    var balloonY:Array = [144, -205, -265, -325];
    var balloonX:Array = [0, 140, 284, 428, 68, 212, 356, 500];

    for (var ballY:uint = 0; ballY < balloonY.length; ballY++){ //Where balloons
        for (var ballX:uint = altX; ballX < altX + BALLOON_ROW; ballX++){ //get added
            var balloonName:String = ("balloon" + count);
            var balloonClip:MovieClip;
            balloonClip = balloonList[count];
            balloonClip.x = balloonX[ballX];
            balloonClip.y = balloonY[ballY];
            balloonClip.name = balloonName;
            addChild(balloonClip);
            trace(balloonClip.parent);
            trace(balloonClip + " has been added!");
            balloonClip.addEventListener(MouseEvent.CLICK, balloonPop); 
            count++;
        }

        if (altX == 0) {
            altX = BALLOON_ROW;
        } else {
            altX = 0;
        }
    }

    function balloonPop(event:MouseEvent):void {
        event.target.play();
        event.target.removeEventListener(MouseEvent.CLICK, balloonPop);
    }
}

我以为我的气球MovieClips可能有问题,所以我把它们放在食物数组中:

var birdSeed:blue_balloon = new blue_balloon;
var catFood:green_balloon = new green_balloon;
var chickenFeed:orange_balloon = new orange_balloon;
var chocolate:purple_balloon = new purple_balloon;
var dogFood:red_balloon = new red_balloon;
var duckFood:yellow_balloon = new yellow_balloon;

他们都出现在舞台上,所以MovieClips没有任何问题。

已添加 balloonXArrayballoonYArray的第一个值分别为-4和-145,但当我开始遇到问题时我想确保气球显示出来,所以我将第一个值设置为0和144,气球的高度和宽度都是144,它们的十字(不确定它的名字)是在左上角。

已添加: balloonList中有多个同一气球实例的原因是因为我需要四行四个气球,但只有六个不同的气球。

我知道气球在舞台上,因为调试显示在可查看的舞台上显示了xy值。使用trace(foodClip.parent)trace(balloonClip.parent)显示气球和食物都有相同的父母MainTimeline,所以我知道气球没有被添加到不同的空间。

我在网上搜索过,但没有遇到过类似问题的人。因此,我在这个论坛上询问是否有人可以告诉我为什么我的气球不会出现在舞台上。

请,谢谢你。

1 个答案:

答案 0 :(得分:0)

我在baloonList中直接看到的一件事是你有多次列出相同的对象实例。每个实例只能在舞台上存在一次。如果addChild()已经在舞台上的实例,则首先删除该实例,然后在显示列表的顶部重新添加。

你应该改变:

var bBalloon:blue_balloon = new blue_balloon;
var gBalloon:green_balloon = new green_balloon;
var oBalloon:orange_balloon = new orange_balloon;
var pBalloon:purple_balloon = new purple_balloon;
var rBalloon:red_balloon = new red_balloon;
var yBalloon:yellow_balloon = new yellow_balloon;

var balloonList:Array = [bBalloon, gBalloon, oBalloon,
    pBalloon, rBalloon, yBalloon, bBalloon, gBalloon,
    oBalloon, pBalloon, rBalloon, yBalloon, bBalloon,
    gBalloon, oBalloon, pBalloon];

为:

var balloonList:Array = [
    new blue_balloon, 
    new green_balloon, 
    new orange_balloon,
    new purple_balloon,
    new red_balloon,
    new yellow_balloon, 
    new blue_balloon, 
    new green_balloon, 
    new orange_balloon,
    new purple_balloon,
    new red_balloon,
    new yellow_balloon, 
    new blue_balloon,
    new blue_balloon, 
    new green_balloon, 
    new orange_balloon,
    new purple_balloon
];