Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Beginning ActionScript 2.0 2006

.pdf
Скачиваний:
105
Добавлен:
17.08.2013
Размер:
12.47 Mб
Скачать

A

Exercise Answers

Chapter 1

There are no actual solutions to the exercises in Chapter 1, which were created simply to help you become accustomed to the Flash IDE, where you’ll be spending quite a bit of time as you work through this book.

Chapter 2

Exercise 1 Solution

a.Valid. It is a combination of a declaration and an assignment, which is allowed in a single statement.

b.Not valid. The data type Integer does not exist. The Number data type should be used instead.

c.Not valid. Strong typing requires use of the var statement.

d.Not valid. A variable name cannot start with a number.

e.Valid, but not recommended. The variable name is also used as a property for the MovieClip class.

f.Not valid. Data of type String is being assigned to a variable of type Boolean. The quotes should be removed.

Exercise 2 Solution

The contents of the round brackets are evaluated first, so numItemsPrepaid is subtracted from numItemsPickedUp. Next, the multiplication is evaluated, with the results of the subtraction multiplied by perItemCost. That result is then added to handlingFee, and that final answer is assigned to totalAmount.

Appendix A

Exercise 3 Solution

The final value is 130.

Exercise 4 Solution

The output is

blue jay-crow-warbler

Chapter 3

Exercise 1 Solution

var shoppingCart:Array = new Array(); shoppingCart.push({modelNumber:”ip300”, description:”MP3 Music Player”, ;

price:299, quantity:1});

shoppingCart.push({modelNumber:”ip300c”, description:”Music Player Case”, ; price:49, quantity:1});

shoppingCart.push({modelNumber:”eb100”, description:”Earbuds”, ; price:29, quantity:2});

shoppingCart.push({modelNumber:”cc250”, description:”Car Charger Kit”, ; price:69, quantity:1});

var totalBill:Number = 0;

var numItems:Number = shoppingCart.length;

for (var i:Number = 0; i < numItems; i++)

{

trace(“Description:

+ shoppingCart[i].description);

trace(“Model Number:

+ shoppingCart[i].modelNumber);

trace(“Price:

 

$” + shoppingCart[i].price);

trace(“Quantity:

 

“ + shoppingCart[i].quantity);

trace(“----------------”);

totalBill = totalBill + shoppingCart[i].price * shoppingCart[i].quantity;

}

 

 

 

trace(“Total Bill:

$” + totalBill);

Exercise 2 Solution

var vehicleType:String = “semi”; var spareCabin:Boolean = false; var numDoors:Number;

switch (vehicleType)

{

case “sedan”: numDoors = 4; break;

case “sportscar”: numDoors = 3; break;

case “semi”:

700

Exercise Answers

if (spareCabin == true)

{

numDoors = 3;

}

else

{

numDoors = 2;

}

break;

default: numDoors = 2;

}

trace(numDoors);

Exercise 3 Solution

var fruitArray:Array = new Array(“banana”, “pear”, “strawberry”, “grape”); var numFruit:Number = fruitArray.length;

var foundFruit:Number = 0; var numLetters:Number = 6;

for (var currentFruit:Number = 0; currentFruit < numFruit; currentFruit++)

{

if (fruitArray[currentFruit].length >= numLetters)

{

trace(fruitArray[currentFruit]);

foundFruit++;

}

}

trace(“found “ + foundFruit + “ fruits with 6 or more letters in the name”);

Chapter 4

Exercise 1 Solution

This solution also makes sure that the comparison is case-insensitive through the addition of the toLower Case() method. Without it, the string Foo would come before bar in the sort order.

function compareStrings(string1:String, string2:String):Number

{

if (string1.toLowerCase() < string2.toLowerCase())

{

return 1;

}

else if (string2.toLowerCase() < string1.toLowerCase())

{

return -1;

}

else

{

return 0;

}

701

Appendix A

}

trace(“comparing foo and bar: “ + compareStrings(“foo”, “bar”));

trace(“comparing boo and far: “ + compareStrings(“boo”, “far”)); trace(“comparing Foo and bar: “ + compareStrings(“Foo”, “bar”)); trace(“comparing foo and foo: “ + compareStrings(“foo”, “foo”));

Exercise 2 Solution

var fruitArray:Array = new Array(“kumquat”, “apple”, “pear”, ; “strawberry”, “banana”);

trace(“FruitArray: “ + fruitArray);

function sortArray(inputArray:Array):Array

{

var tempArray:Array = inputArray.slice(); // Makes a complete copy.

tempArray.sort(); return tempArray;

}

var sortedArray:Array = sortArray(fruitArray); trace(“FruitArray: “ + fruitArray); trace(“SortedArray: “ + sortedArray);

Exercise 3 Solution

function drawRectangle(targetMC:MovieClip, rectWidth:Number, rectHeight:Number, bgColor:Number):Void

{

targetMC.moveTo(0, 0); targetMC.lineStyle(1, 0x000000); targetMC.beginFill(bgColor, 100); targetMC.lineTo(rectWidth, 0); targetMC.lineTo(rectWidth, rectHeight); targetMC.lineTo(0, rectHeight); targetMC.lineTo(0, 0);

}

function buttonClickHandler():Void

{

trace(“Button pressed”);

}

function convertToButton(targetMC:MovieClip, callbackFunction:Function):Void

{

drawRectangle(targetMC, 100, 30, 0x333399);

targetMC.onPress = function()

{

this.clear();

drawRectangle(this, 100, 30, 0xCC3333);

}

targetMC.onRelease = function()

{

702

Exercise Answers

this.clear();

drawRectangle(this, 100, 30, 0x6666AA); callbackFunction();

}

targetMC.onReleaseOutside = function()

{

this.clear();

drawRectangle(this, 100, 30, 0x333399);

}

targetMC.onRollOver = function()

{

this.clear();

drawRectangle(this, 100, 30, 0x6666AA);

}

targetMC.onRollOut = function()

{

this.clear();

drawRectangle(this, 100, 30, 0x333399);

}

}

this.createEmptyMovieClip(“testButton”, 1); convertToButton(testButton, buttonClickHandler);

Chapter 5

Exercise 1 Solution

a.Valid. This is explicit string creation.

b.Valid. This is implicit string creation.

c.The syntax is valid, but the data types are not correct. The Date object does not accept String input.

d.Valid.

e.Valid. This is an implicit creation of an Object.

f.Valid. The Object data type is the parent from which all other data types are created. This will generate no type warnings, regardless of what type of data is assigned to the variable.

g.Valid, but not recommended. The variable creation does not use strong typing.

h.Invalid. A string is being assigned to a variable of type Number.

Exercise 2 Solution

Outputs:

a.

b.

c.

testFunction->myString: qux

 

testFunction->mySecondString: foo

 

myString: baz

703

 

Appendix A

d.

e.

mySecondString: foo

mySecondString: bar

Exercise 3 Solution

a.Valid.

b.Valid.

c.Invalid. A return type of String has been declared, but there is no return statement.

d.Invalid. A return type of String has been declared, but the function is returning a Boolean value.

e.Valid. A return type of Void indicates no data is to be returned.

f.Valid, but not recommended. No return type has been specified.

Exercise 4 Solution

function invokePlay():Void

{

trace(“Pressed play”); presentationArea.play();

}

function invokeStop():Void

{

trace(“Pressed stop”); presentationArea.stop();

}

function setupMovie():Void

{

// Create presentation area, and load a movie clip into it this.createEmptyMovieClip(“presentationArea”, 3); presentationArea._x = 10;

presentationArea._y = 40; presentationArea.loadMovie(“http://www.nathanderksen.com/book/;

tryItOut_refactorAnimation.swf”);

}

function createButton(parentMovieClip:MovieClip, buttonName:String, ; buttonLabel:String, xPos:Number, yPos:Number, buttonWidth:Number, ; buttonHeight:Number, callback:Function):Void

{

var buttonHandle:MovieClip; parentMovieClip.createEmptyMovieClip(buttonName, ;

parentMovieClip.getNextHighestDepth()); buttonHandle = parentMovieClip[buttonName]; buttonHandle.onRelease = function()

{

callback();

}

buttonHandle._x = xPos; buttonHandle._y = yPos;

drawBox(buttonHandle, buttonWidth, buttonHeight, 0xAAAAFF, 0x333333);

704

Exercise Answers

buttonHandle.createTextField(“labelField”, 1, 4, 4, 50, 15); buttonHandle.labelField.text = buttonLabel;

}

function drawBox(targetMovieClip:MovieClip, boxWidth:Number, boxHeight:Number, ; boxColor:Number, lineColor:Number):Void

{

targetMovieClip.beginFill(boxColor); targetMovieClip.lineStyle(1, lineColor); targetMovieClip.moveTo(0, 0); targetMovieClip.lineTo(boxWidth, 0); targetMovieClip.lineTo(boxWidth, boxHeight); targetMovieClip.lineTo(0, boxHeight); targetMovieClip.lineTo(0, 0);

}

createButton(this, “playButton”, “Play”, 10, 10, 70, 20, invokePlay); createButton(this, “stopButton”, “Stop”, 90, 10, 35, 20, invokeStop); setupMovie();

Chapter 6

Exercise Solution

1.Make note of the x and y coordinates for the four buttons in the screen_photography library clip, and then delete them.

2.Give the movie clip holding the large image preview the instance name imageHolderClip.

3.Go to the library and assign linkage IDs for the button_aStudyInTexture, button_buntzenWinter, button_flowerInDetail, and button_galianoSunset library symbols.

4.Update the code to resemble the following:

var buttonArray:Array = new Array(); buttonArray.push(“aStudyInTexture”); buttonArray.push(“buntzenWinter”); buttonArray.push(“flowerInDetail”); buttonArray.push(“galianoSunset”);

// Set up code to respond to main menu buttons home_btn.onRelease = function()

{

gotoScreen(“home”);

}

tutorials_btn.onRelease = function()

{

gotoScreen(“tutorials”);

}

photography_btn.onRelease = function()

{

gotoScreen(“photography”);

705

Appendix A

}

function gotoScreen(screenName:String):Void

{

var screenHolderHandle:MovieClip; screenHolderHandle = _level0[“screenHolder”];

//Hide all screens within the screen holder movie clip for (screen in screenHolderHandle)

{

screenHolderHandle[screen]._visible = false;

}

//Show the selected screen screenHolderHandle[screenName]._visible = true;

}

function init():Void

{

var screenHolderHandle:MovieClip; var buttonHandle:MovieClip;

_level0.createEmptyMovieClip(“screenHolder”, _level0.getNextHighestDepth()); screenHolderHandle = _level0[“screenHolder”]; screenHolderHandle.attachMovie(“screen_home”, “home”, ;

screenHolderHandle.getNextHighestDepth()); screenHolderHandle.attachMovie(“screen_tutorials”, “tutorials”, ;

screenHolderHandle.getNextHighestDepth()); screenHolderHandle.attachMovie(“screen_photography”, “photography”, ;

screenHolderHandle.getNextHighestDepth());

gotoScreen(“home”);

// Attach the photo buttons to the photography screen movie clip. for (var i:Number = 0; i < buttonArray.length; i++)

{

buttonHandle = screenHolderHandle.photography.attachMovie(“button_” + ; buttonArray[i], “button_” + buttonArray[i], ; screenHolderHandle.photography.getNextHighestDepth());

buttonHandle._x = 25; buttonHandle._y = 160 + (60 * i); buttonHandle.id = buttonArray[i]; buttonHandle.onRelease = function()

{

loadImage(this.id);

}

}

}

function loadImage(imageName:String)

{

screenHolder.photography.imageHolderClip.loadMovie(“images/screens/; photography/” + imageName + “_preview.jpg”);

}

init();

706

Exercise Answers

Chapter 7

Exercise 1 Solution

var currentYear:Number; var currentMonth:Number; var currentDate:Number;

// Associates month names with the month number

var monthArray = new Array(“January”, “February”, “March”, “April”, “May”, ; “June”, “July”, “August”, “September”, “October”, “November”, “December”);

function createButton(buttonName:String, buttonLabel:String, ; buttonData:String, parentClip:MovieClip, xPos:Number, yPos:Number, ; buttonWidth:Number, buttonHeight:Number, buttonHandler:Function) : Void

{

var newClip:MovieClip = parentClip.createEmptyMovieClip(buttonName, ; parentClip.getNextHighestDepth());

newClip._x = xPos; newClip._y = yPos;

newClip.moveTo(0, 0); newClip.lineStyle(1, 0x666666); newClip.beginFill(0xFFFFFF, 100); newClip.lineTo(buttonWidth, 0); newClip.lineTo(buttonWidth, buttonHeight); newClip.lineTo(0, buttonHeight); newClip.lineTo(0, 0);

newClip.endFill();

newClip.createTextField(“labelField”, newClip.getNextHighestDepth(), 2, ; buttonHeight/2 - 7, buttonWidth - 4, 14);

newClip.labelField.text = buttonLabel; newClip.buttonData = buttonData; newClip.onRelease = function()

{

// Invokes a function that was passed as an argument buttonHandler(this.buttonData);

}

}

function deleteButton(buttonName:String):Void

{

this[buttonName].removeMovieClip();

}

function setWeek(year:Number, month:Number, date:Number):Void

{

var thisDate:Date = new Date(year, month, date); monthYearLabel.text = monthArray[month] + “ “ + year;

for (var i:Number = 0; i < 7; i++)

{

deleteButton(“day” + i + “button”);

707