AS 2 App Doesn't Display Dynamic Image Properly - Help

  • earthlie
  • Born
  • Born
  • earthlie
  • Posts: 1

Post 3+ Months Ago

I recently created a visual quiz app which asks a question and then gives 4 image answers.

The problem that I'm having is that the images don't shrink the image into their quadrants, but rather, zooms in on the top left corner of the image.

Can anyone tell me what I have to change in the following code to make the images display at a set width (so that they fit in the quadrants proportionally).

Here is what the SWF looks like now. As you can see the images become distorted because they are being stretched. What should happen is that the images be displayed at a fixed width and variable height.
Image

Code: [ Select ]
var dataXML:XML = new XML();
var arrSounds:Array = new Array();
var arrData:Array = new Array();
var rndData:Array = new Array();
var soundID = 0;
var imgCur = 0;
var isRandom = false;
var imgLoader:MovieClipLoader = new MovieClipLoader();
var mclListener:Object = new Object();
this.createEmptyMovieClip("sounds", this.getNextHighestDepth());
mcanswer._visible = false;
RandomArray();
//----------
dataXML.ignoreWhite = true;
dataXML.onLoad = function(success:Boolean) {
    if (success) {
        var tmp = dataXML.firstChild.childNodes;
        //_global.audiopath = dataXML.firstChild.attributes.audiopath;
        //_global.imagepath = dataXML.firstChild.attributes.imagepath;
        mcanswer.Correct.Title.text = dataXML.firstChild.attributes.text_correct;
        mcanswer.Incorrect.Title.text = dataXML.firstChild.attributes.text_incorrect;
        if (dataXML.firstChild.attributes.randomize == "true") {
            isRandom = true;
        }
        var t = 0;
        for (i=0; i<tmp.length; i++) {
            var obj:Object = new Object();
            if (isRandom) {
                if ((tmp[i].attributes.type == "question") || (tmp[i].attributes.type == "direction")) {
                    obj.type = tmp[i].attributes.type;
                } else {
                    obj.type = "answer"+rndData[t];
                    t++;
                }
            } else {
                obj.type = tmp[i].attributes.type;
            }
            obj.text = tmp[i].firstChild.nodeValue;
            obj.feedback = CheckString(tmp[i].attributes.feedback);
            obj.image = CheckString(tmp[i].attributes.image);
            obj.audio = CheckString(tmp[i].attributes.sound);
            obj.status = CheckString(tmp[i].attributes.status);
            arrData.push(obj);
        }
        //-------------
        //Randomize();
        LoadImages();
        setSpeakerActions();
        setTexts();
    } else {
        trace("Error: loading xml file");
    }
};
dataXML.load(_root.xmlPath);
//dataXML.load("question.xml");
function RandomArray() {
    var tmpArray = new Array();
    i = 0;
    while (i<4) {
        z = random(4);
        if (tmpArray[z] != 55) {
            rndData[i] = z+1;
            tmpArray[z] = 55;
            i++;
        }
    }
}
function LoadSounds() {
    if (soundID<arrData.length) {
        if (arrData[soundID].audio != null) {
            soundloading._visible = true;
            var mc = sounds.createEmptyMovieClip("audio_"+arrData[soundID].type, sounds.getNextHighestDepth());
            mc.s = new Sound();
            mc.s.loadSound(arrData[soundID].audio, false);
            loading.Status.text = "Please, wait while loading sounds...\n";
            loading.Status.text += "Loading "+soundID+" of "+arrSounds.length;
            mc.s.onLoad = function(success:Boolean) {
                if (success) {
                    soundloading._visible = false;
                    if (soundID<arrData.length) {
                        soundID++;
                        LoadSounds();
                    } else {
                        //trace("finished");
                        soundloading._visible = false;
                    }
                    //this.start();
                    //trace("Sound loaded");
                } else {
                    //trace("Sound failed");
                    soundID++;
                    LoadSounds();
                }
            };
        } else {
            soundID++;
            LoadSounds();
        }
    } else {
        // loading finished
        //soundID++;
        //LoadSounds();
    }
}
function setSpeakerActions() {
    for (i=0; i<arrData.length; i++) {
        var mc = eval("audio_"+arrData[i].type);
        var mcs = eval("image_"+arrData[i].type+".bigspeaker");
        var mcimg = eval("image_"+arrData[i].type);
        mcs._visible = false;
        if (arrData[i].audio == null) {
            mc._visible = false;
        } else {
            mc.onRelease = function() {
                stopAllSounds();
                sounds[this._name].s.start();
            };
        }
        if (arrData[i].image == null) {
            if (arrData[i].type != "direction") {
                mcimg._visible = false;
                var mctxt = eval("text_"+arrData[i].type);
                mc._x = mctxt._x=mcimg._x;
            }
        }
    }
}
function setTexts() {
    for (i=0; i<arrData.length; i++) {
        var mc = eval("text_"+arrData[i].type);
        if (arrData[i].text == null) {
            mc._visible = false;
        } else {
            mc.txt.text = arrData[i].text;
        }
        if (arrData[i].type == "direction") {
            var my_fmt:TextFormat = new TextFormat();
            my_fmt.size = 12;
            mc.txt.setTextFormat(my_fmt);
            audio_direction._x = mc._width+30;
            audio_direction._y = mc._y;
        }
    }
}
function CheckString(str) {
    if (str == "" || str == undefined) {
        return null;
    } else {
        return str;
    }
}
mclListener.onLoadComplete = function(targetMC) {
};
mclListener.onLoadError = function(target_mc:MovieClip, errorCode:String, httpStatus:Number) {
    imgCur++;
    LoadImages();
    trace("error:"+errorCode);
};
mclListener.onLoadInit = function(target_mc:MovieClip) {
    //----------
    if (target_mc._parent._name != "image_question") {
        target_mc._parent._alpha = 80;
    }
    imgCur++;
    LoadImages();
};
imgLoader.addListener(mclListener);
function LoadImages() {
    if (imgCur<arrData.length) {
        tmpName = "img_"+imgCur;
        target = eval("image_"+arrData[imgCur].type+".image");
        eval("image_"+arrData[imgCur].type+".image")._x = -10+((imgCur-1)*1);
        if (arrData[imgCur].image !== null) {
            imgLoader.loadClip(arrData[imgCur].image, target);
            var pimg = Math.round((100*(imgCur+1))/arrData.length);
            mcloading.bar.gotoAndStop(pimg);         
        } else {
            imgCur++;
            LoadImages();
        }
    } else {
        //------
        // loading images finished, load sounds
        mcloading._visible = false;
        LoadSounds();
    }
}
//---------------
// answer actions
//-----------------
image_answer1.onRelease = text_answer1.onRelease=function () {
    CheckAnswer("answer1");
};
image_answer2.onRelease = text_answer2.onRelease=function () {
    CheckAnswer("answer2");
};
image_answer3.onRelease = text_answer3.onRelease=function () {
    CheckAnswer("answer3");
};
image_answer4.onRelease = text_answer4.onRelease=function () {
    CheckAnswer("answer4");
};
image_answer1.onRollOver = text_answer1.onRollOver=function () {
    SetAlpha(image_answer1, text_answer1, 100);

};
image_answer2.onRollOver = text_answer2.onRollOver=function () {
    SetAlpha(image_answer2, text_answer2, 100);
};
image_answer3.onRollOver = text_answer3.onRollOver=function () {
    SetAlpha(image_answer3, text_answer3, 100);
};
image_answer4.onRollOver = text_answer4.onRollOver=function () {
    SetAlpha(image_answer4, text_answer4, 100);
};
image_answer1.onRollOut = text_answer1.onRollOut=function () {
    SetAlpha(image_answer1, text_answer1, 80);
};
image_answer2.onRollOut = text_answer2.onRollOut=function () {
    SetAlpha(image_answer2, text_answer2, 80);
};
image_answer3.onRollOut = text_answer3.onRollOut=function () {
    SetAlpha(image_answer3, text_answer3, 80);
};
image_answer4.onRollOut = text_answer4.onRollOut=function () {
    SetAlpha(image_answer4, text_answer4, 80);
};
mcanswer.bg.useHandCursor = false;
mcanswer.bg.onRelease = function() {
};
mcanswer.Incorrect.Close.onRelease = function() {
    mcanswer._visible = false;
};
mcanswer.Correct.Close.onRelease = mcanswer.Incorrect.Close.onRelease;
function SetAlpha(mc1, mc2, a) {
    mc1._alpha = a;
    var my_fmt:TextFormat = new TextFormat();
    if (a == 100) {
        my_fmt.color = 0x999999;
    } else {
        my_fmt.color = 0x535353;
    }
    mc2.txt.setTextFormat(my_fmt);
}
function CheckAnswer(ans) {
    var answer = false;
    var feedback = "";
    for (i=0; i<arrData.length; i++) {
        if (arrData[i].status == "correct" && arrData[i].type == ans) {
            answer = true;
        }
        if (arrData[i].type == ans) {
            feedback = arrData[i].feedback;
        }
    }
    if (answer) {
        mcanswer.Incorrect._visible = false;
        mcanswer.Correct._visible = true;
        mcanswer._visible = true;
    } else {
        mcanswer.Incorrect._visible = true;
        mcanswer.Correct._visible = false;
        mcanswer._visible = true;
        mcanswer.Incorrect.txt.text = feedback;
    }
}
  1. var dataXML:XML = new XML();
  2. var arrSounds:Array = new Array();
  3. var arrData:Array = new Array();
  4. var rndData:Array = new Array();
  5. var soundID = 0;
  6. var imgCur = 0;
  7. var isRandom = false;
  8. var imgLoader:MovieClipLoader = new MovieClipLoader();
  9. var mclListener:Object = new Object();
  10. this.createEmptyMovieClip("sounds", this.getNextHighestDepth());
  11. mcanswer._visible = false;
  12. RandomArray();
  13. //----------
  14. dataXML.ignoreWhite = true;
  15. dataXML.onLoad = function(success:Boolean) {
  16.     if (success) {
  17.         var tmp = dataXML.firstChild.childNodes;
  18.         //_global.audiopath = dataXML.firstChild.attributes.audiopath;
  19.         //_global.imagepath = dataXML.firstChild.attributes.imagepath;
  20.         mcanswer.Correct.Title.text = dataXML.firstChild.attributes.text_correct;
  21.         mcanswer.Incorrect.Title.text = dataXML.firstChild.attributes.text_incorrect;
  22.         if (dataXML.firstChild.attributes.randomize == "true") {
  23.             isRandom = true;
  24.         }
  25.         var t = 0;
  26.         for (i=0; i<tmp.length; i++) {
  27.             var obj:Object = new Object();
  28.             if (isRandom) {
  29.                 if ((tmp[i].attributes.type == "question") || (tmp[i].attributes.type == "direction")) {
  30.                     obj.type = tmp[i].attributes.type;
  31.                 } else {
  32.                     obj.type = "answer"+rndData[t];
  33.                     t++;
  34.                 }
  35.             } else {
  36.                 obj.type = tmp[i].attributes.type;
  37.             }
  38.             obj.text = tmp[i].firstChild.nodeValue;
  39.             obj.feedback = CheckString(tmp[i].attributes.feedback);
  40.             obj.image = CheckString(tmp[i].attributes.image);
  41.             obj.audio = CheckString(tmp[i].attributes.sound);
  42.             obj.status = CheckString(tmp[i].attributes.status);
  43.             arrData.push(obj);
  44.         }
  45.         //-------------
  46.         //Randomize();
  47.         LoadImages();
  48.         setSpeakerActions();
  49.         setTexts();
  50.     } else {
  51.         trace("Error: loading xml file");
  52.     }
  53. };
  54. dataXML.load(_root.xmlPath);
  55. //dataXML.load("question.xml");
  56. function RandomArray() {
  57.     var tmpArray = new Array();
  58.     i = 0;
  59.     while (i<4) {
  60.         z = random(4);
  61.         if (tmpArray[z] != 55) {
  62.             rndData[i] = z+1;
  63.             tmpArray[z] = 55;
  64.             i++;
  65.         }
  66.     }
  67. }
  68. function LoadSounds() {
  69.     if (soundID<arrData.length) {
  70.         if (arrData[soundID].audio != null) {
  71.             soundloading._visible = true;
  72.             var mc = sounds.createEmptyMovieClip("audio_"+arrData[soundID].type, sounds.getNextHighestDepth());
  73.             mc.s = new Sound();
  74.             mc.s.loadSound(arrData[soundID].audio, false);
  75.             loading.Status.text = "Please, wait while loading sounds...\n";
  76.             loading.Status.text += "Loading "+soundID+" of "+arrSounds.length;
  77.             mc.s.onLoad = function(success:Boolean) {
  78.                 if (success) {
  79.                     soundloading._visible = false;
  80.                     if (soundID<arrData.length) {
  81.                         soundID++;
  82.                         LoadSounds();
  83.                     } else {
  84.                         //trace("finished");
  85.                         soundloading._visible = false;
  86.                     }
  87.                     //this.start();
  88.                     //trace("Sound loaded");
  89.                 } else {
  90.                     //trace("Sound failed");
  91.                     soundID++;
  92.                     LoadSounds();
  93.                 }
  94.             };
  95.         } else {
  96.             soundID++;
  97.             LoadSounds();
  98.         }
  99.     } else {
  100.         // loading finished
  101.         //soundID++;
  102.         //LoadSounds();
  103.     }
  104. }
  105. function setSpeakerActions() {
  106.     for (i=0; i<arrData.length; i++) {
  107.         var mc = eval("audio_"+arrData[i].type);
  108.         var mcs = eval("image_"+arrData[i].type+".bigspeaker");
  109.         var mcimg = eval("image_"+arrData[i].type);
  110.         mcs._visible = false;
  111.         if (arrData[i].audio == null) {
  112.             mc._visible = false;
  113.         } else {
  114.             mc.onRelease = function() {
  115.                 stopAllSounds();
  116.                 sounds[this._name].s.start();
  117.             };
  118.         }
  119.         if (arrData[i].image == null) {
  120.             if (arrData[i].type != "direction") {
  121.                 mcimg._visible = false;
  122.                 var mctxt = eval("text_"+arrData[i].type);
  123.                 mc._x = mctxt._x=mcimg._x;
  124.             }
  125.         }
  126.     }
  127. }
  128. function setTexts() {
  129.     for (i=0; i<arrData.length; i++) {
  130.         var mc = eval("text_"+arrData[i].type);
  131.         if (arrData[i].text == null) {
  132.             mc._visible = false;
  133.         } else {
  134.             mc.txt.text = arrData[i].text;
  135.         }
  136.         if (arrData[i].type == "direction") {
  137.             var my_fmt:TextFormat = new TextFormat();
  138.             my_fmt.size = 12;
  139.             mc.txt.setTextFormat(my_fmt);
  140.             audio_direction._x = mc._width+30;
  141.             audio_direction._y = mc._y;
  142.         }
  143.     }
  144. }
  145. function CheckString(str) {
  146.     if (str == "" || str == undefined) {
  147.         return null;
  148.     } else {
  149.         return str;
  150.     }
  151. }
  152. mclListener.onLoadComplete = function(targetMC) {
  153. };
  154. mclListener.onLoadError = function(target_mc:MovieClip, errorCode:String, httpStatus:Number) {
  155.     imgCur++;
  156.     LoadImages();
  157.     trace("error:"+errorCode);
  158. };
  159. mclListener.onLoadInit = function(target_mc:MovieClip) {
  160.     //----------
  161.     if (target_mc._parent._name != "image_question") {
  162.         target_mc._parent._alpha = 80;
  163.     }
  164.     imgCur++;
  165.     LoadImages();
  166. };
  167. imgLoader.addListener(mclListener);
  168. function LoadImages() {
  169.     if (imgCur<arrData.length) {
  170.         tmpName = "img_"+imgCur;
  171.         target = eval("image_"+arrData[imgCur].type+".image");
  172.         eval("image_"+arrData[imgCur].type+".image")._x = -10+((imgCur-1)*1);
  173.         if (arrData[imgCur].image !== null) {
  174.             imgLoader.loadClip(arrData[imgCur].image, target);
  175.             var pimg = Math.round((100*(imgCur+1))/arrData.length);
  176.             mcloading.bar.gotoAndStop(pimg);         
  177.         } else {
  178.             imgCur++;
  179.             LoadImages();
  180.         }
  181.     } else {
  182.         //------
  183.         // loading images finished, load sounds
  184.         mcloading._visible = false;
  185.         LoadSounds();
  186.     }
  187. }
  188. //---------------
  189. // answer actions
  190. //-----------------
  191. image_answer1.onRelease = text_answer1.onRelease=function () {
  192.     CheckAnswer("answer1");
  193. };
  194. image_answer2.onRelease = text_answer2.onRelease=function () {
  195.     CheckAnswer("answer2");
  196. };
  197. image_answer3.onRelease = text_answer3.onRelease=function () {
  198.     CheckAnswer("answer3");
  199. };
  200. image_answer4.onRelease = text_answer4.onRelease=function () {
  201.     CheckAnswer("answer4");
  202. };
  203. image_answer1.onRollOver = text_answer1.onRollOver=function () {
  204.     SetAlpha(image_answer1, text_answer1, 100);
  205. };
  206. image_answer2.onRollOver = text_answer2.onRollOver=function () {
  207.     SetAlpha(image_answer2, text_answer2, 100);
  208. };
  209. image_answer3.onRollOver = text_answer3.onRollOver=function () {
  210.     SetAlpha(image_answer3, text_answer3, 100);
  211. };
  212. image_answer4.onRollOver = text_answer4.onRollOver=function () {
  213.     SetAlpha(image_answer4, text_answer4, 100);
  214. };
  215. image_answer1.onRollOut = text_answer1.onRollOut=function () {
  216.     SetAlpha(image_answer1, text_answer1, 80);
  217. };
  218. image_answer2.onRollOut = text_answer2.onRollOut=function () {
  219.     SetAlpha(image_answer2, text_answer2, 80);
  220. };
  221. image_answer3.onRollOut = text_answer3.onRollOut=function () {
  222.     SetAlpha(image_answer3, text_answer3, 80);
  223. };
  224. image_answer4.onRollOut = text_answer4.onRollOut=function () {
  225.     SetAlpha(image_answer4, text_answer4, 80);
  226. };
  227. mcanswer.bg.useHandCursor = false;
  228. mcanswer.bg.onRelease = function() {
  229. };
  230. mcanswer.Incorrect.Close.onRelease = function() {
  231.     mcanswer._visible = false;
  232. };
  233. mcanswer.Correct.Close.onRelease = mcanswer.Incorrect.Close.onRelease;
  234. function SetAlpha(mc1, mc2, a) {
  235.     mc1._alpha = a;
  236.     var my_fmt:TextFormat = new TextFormat();
  237.     if (a == 100) {
  238.         my_fmt.color = 0x999999;
  239.     } else {
  240.         my_fmt.color = 0x535353;
  241.     }
  242.     mc2.txt.setTextFormat(my_fmt);
  243. }
  244. function CheckAnswer(ans) {
  245.     var answer = false;
  246.     var feedback = "";
  247.     for (i=0; i<arrData.length; i++) {
  248.         if (arrData[i].status == "correct" && arrData[i].type == ans) {
  249.             answer = true;
  250.         }
  251.         if (arrData[i].type == ans) {
  252.             feedback = arrData[i].feedback;
  253.         }
  254.     }
  255.     if (answer) {
  256.         mcanswer.Incorrect._visible = false;
  257.         mcanswer.Correct._visible = true;
  258.         mcanswer._visible = true;
  259.     } else {
  260.         mcanswer.Incorrect._visible = true;
  261.         mcanswer.Correct._visible = false;
  262.         mcanswer._visible = true;
  263.         mcanswer.Incorrect.txt.text = feedback;
  264.     }
  265. }
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

I would highly recommend NOT scaling images in flash. It typically does a very bad job. Instead either create the images at the size you need or use a mask to hide the excess from view.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 12 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.