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

Beginning ActionScript 2.0 2006

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

Appendix A

createButton(“day” + i + “button”, String(thisDate.getDate()), ; thisDate.toString(), this, 60 + (i * 30), 40, 20, 20, ; handleDateSelected);

thisDate.setDate(thisDate.getDate() + 1);

}

}

function handlePreviousWeek():Void

{

var thisDate:Date = new Date(currentYear, currentMonth, currentDate); thisDate.setDate(thisDate.getDate() - 7);

currentYear = thisDate.getYear() + 1900; currentMonth = thisDate.getMonth(); currentDate = thisDate.getDate();

setWeek(currentYear, currentMonth, currentDate);

}

function handleNextWeek():Void

{

var thisDate:Date = new Date(currentYear, currentMonth, currentDate); thisDate.setDate(thisDate.getDate() + 7);

currentYear = thisDate.getYear() + 1900; currentMonth = thisDate.getMonth(); currentDate = thisDate.getDate();

setWeek(currentYear, currentMonth, currentDate);

}

function handleDateSelected(selectedDate:String):Void

{

trace(“handleDate: “ + selectedDate);

}

function init():Void

{

// Set up screen, except for day buttons to be created fresh for each new week. createButton(“previousWeekButton”, “<<”, “”, this, 30, 40, 20, 20, ;

handlePreviousWeek);

createButton(“nextWeekButton”, “>>”, “”, this, 270, 40, 20, 20, ; handleNextWeek);

this.createTextField(“day0Label”, this.getNextHighestDepth(), 65, 25, 20, 20); this.day0Label.text = “S”;

this.createTextField(“day1Label”, this.getNextHighestDepth(), 95, 25, 20, 20); this.day1Label.text = “M”;

this.createTextField(“day2Label”, this.getNextHighestDepth(), 125, 25, 20, 20); this.day2Label.text = “T”;

this.createTextField(“day3Label”, this.getNextHighestDepth(), 155, 25, 20, 20); this.day3Label.text = “W”;

this.createTextField(“day4Label”, this.getNextHighestDepth(), 185, 25, 20, 20); this.day4Label.text = “T”;

this.createTextField(“day5Label”, this.getNextHighestDepth(), 215, 25, 20, 20); this.day5Label.text = “F”;

708

Exercise Answers

this.createTextField(“day6Label”, this.getNextHighestDepth(), 245, 25, 20, 20); this.day6Label.text = “S”;

this.createTextField(“monthYearLabel”, this.getNextHighestDepth(), ; 130, 5, 150, 20);

//Initialize the current date. Adjust today’s date so that it actually

//gets the date from the previous Sunday, so that the days line up

//properly with the day labels. If it is Wednesday, getDay() will

//return 3. Subtracting 3 from the date will get Sunday’s date.

var todaysDate:Date = new Date(); todaysDate.setDate(todaysDate.getDate() - todaysDate.getDay());

currentYear = todaysDate.getYear() + 1900; currentMonth = todaysDate.getMonth(); currentDate = todaysDate.getDate();

setWeek(currentYear, currentMonth, currentDate);

}

init();

Exercise 2 Solution

this.createEmptyMovieClip(“squareClip”, this.getNextHighestDepth()); squareClip.lineStyle(1, 0x000000);

squareClip.moveTo(0, 0); squareClip.beginFill(0xCC0000, 100); squareClip.lineTo(0, 100); squareClip.lineTo(100, 100); squareClip.lineTo(100, 0); squareClip.lineTo(0, 0);

this.createEmptyMovieClip(“triangleClip”, this.getNextHighestDepth()); triangleClip._x = 30;

triangleClip._y = 30; triangleClip.lineStyle(1, 0x000000); triangleClip.moveTo(0, 100); triangleClip.beginFill(0x0000CC, 100); triangleClip.lineTo(100, 100); triangleClip.lineTo(50, 0); triangleClip.lineTo(0, 100);

squareClip.setMask(triangleClip); // Part 2: Swapped masks: //triangleClip.setMask(squareClip);

Exercise 3 Solution

this.createEmptyMovieClip(“squareClip”, this.getNextHighestDepth()); squareClip._x = 30;

squareClip._y = 30; squareClip.lineStyle(1, 0x000000); squareClip.moveTo(0, 0); squareClip.beginFill(0xCC0000, 100); squareClip.lineTo(0, 100);

709

Appendix A

squareClip.lineTo(100, 100); squareClip.lineTo(100, 0); squareClip.lineTo(0, 0);

this.createEmptyMovieClip(“triangleClip”, this.getNextHighestDepth()); triangleClip._x = 30;

triangleClip._y = 30; triangleClip.lineStyle(1, 0x000000); triangleClip.moveTo(0, 100); triangleClip.beginFill(0x0000CC, 100); triangleClip.lineTo(100, 100); triangleClip.lineTo(50, 0);

triangleClip.lineTo(0, 100);

squareClip.setMask(triangleClip);

var mouseListener:Object = new Object(); mouseListener.onMouseMove = function ()

{

trace(“x: “ + _xmouse); triangleClip._x = _xmouse - 50; triangleClip._y = _ymouse - 50;

};

Mouse.addListener(mouseListener);

Chapter 8

Exercise 1 Solution

this.createEmptyMovieClip(“movieHolder”, this.getNextHighestDepth()); this.createEmptyMovieClip(“progressMovie”, this.getNextHighestDepth()); progressMovie._x = 60;

progressMovie._y = 120; progressMovie.createTextField(“percentDoneLabel”, progressMovie.getNextHighestDepth(), 40, 0, 200, 20);

var loadListener:Object = new Object(); loadListener.onLoadStart = function(contentHolder:MovieClip)

{

progressMovie._visible = true; contentHolder._visible = false; contentHolder.stop();

};

loadListener.onLoadProgress = function(contentHolder:MovieClip, ; bytesLoaded:Number, bytesTotal:Number)

{

var percentDone:Number = bytesLoaded / bytesTotal * 100; showProgress(percentDone, progressMovie);

};

loadListener.onLoadComplete = function(contentHolder:MovieClip)

{

progressMovie._visible = false;

710

Exercise Answers

contentHolder._visible = true; contentHolder.play();

};

loadListener.onLoadError = function(contentHolder:MovieClip, errorCode:String)

{

if (errorCode == “URLNotFound”)

{

progressMovie.percentDoneLabel.text = “ERROR: Could not load media”; progressMovie.clear();

}

else if (errorCode == “LoadNeverCompleted”)

{

progressMovie.percentDoneLabel.text = “ERROR: Transfer interrupted”; progressMovie.clear();

}

}

var contentLoader:MovieClipLoader = new MovieClipLoader(); contentLoader.addListener(loadListener); contentLoader.loadClip(“<your server>/trailer.swf”, ;

movieHolder); //contentLoader.loadClip(“clipThatDoesNotExist.swf”, movieHolder);

function showProgress(percentDone:Number, progressBarHolder:MovieClip) : Void

{

var barWidth:Number = percentDone;

progressBarHolder.percentDoneLabel.text = String(Math.ceil(percentDone))+” %”; progressBarHolder.clear();

//Draw a border progressBarHolder.moveTo(0, 20); progressBarHolder.lineStyle(1, 0x666666); progressBarHolder.lineTo(100, 20); progressBarHolder.lineTo(100, 30); progressBarHolder.lineTo(0, 30); progressBarHolder.lineTo(0, 20);

//Draw the bar progressBarHolder.moveTo(0, 20); progressBarHolder.beginFill(0xCCCCCC, 100); progressBarHolder.lineTo(barWidth, 20); progressBarHolder.lineTo(barWidth, 30); progressBarHolder.lineTo(0, 30); progressBarHolder.lineTo(0, 20); progressBarHolder.endFill();

}

Exercise 2 Solution

Before trying the code, make sure to place a ProgressBar component in the library.

this.createEmptyMovieClip(“movieHolder”, this.getNextHighestDepth()); // Requires: a ProgressBar component placed in the library this.createClassObject(mx.controls.ProgressBar, “progressComponent”, ;

711

Appendix A

this.getNextHighestDepth());

progressComponent._x = 60; progressComponent._y = 120; progressComponent.mode = “manual”;

var loadListener:Object = new Object(); loadListener.onLoadStart = function(contentHolder:MovieClip)

{

progressComponent._visible = true; progressComponent.setProgress(0, 100); contentHolder._visible = false; contentHolder.stop();

};

loadListener.onLoadProgress = function(contentHolder:MovieClip, ; bytesLoaded:Number, bytesTotal:Number)

{

progressComponent.setProgress(bytesLoaded, bytesTotal);

};

loadListener.onLoadComplete = function(contentHolder:MovieClip)

{

progressComponent._visible = false; contentHolder._visible = true; contentHolder.play();

};

loadListener.onLoadError = function(contentHolder:MovieClip, errorCode:String)

{

if (errorCode == “URLNotFound”)

{

progressComponent.label = “Could not load media”;

}

else if (errorCode == “LoadNeverCompleted”)

{

progressComponent.label = “Transfer interrupted”;

}

};

var contentLoader:MovieClipLoader = new MovieClipLoader(); contentLoader.addListener(loadListener); contentLoader.loadClip(“<your server>/trailer.swf”, movieHolder);

Chapter 9

Exercise 1 Solution

Before trying the code, make sure to place a Button, a DataGrid, a Label, a TextArea, and a TextInput component in the library.

import mx.controls.Button; import mx.controls.DataGrid; import mx.controls.Label;

712

Exercise Answers

import mx.controls.TextArea; import mx.controls.TextInput;

function drawScreen():Void

{

this.createClassObject(Label, “gridTitle”, this.getNextHighestDepth()); gridTitle.text = “Notes”;

gridTitle._x = 18; gridTitle._y = 18;

this.createClassObject(DataGrid, “notesDataGrid”, this.getNextHighestDepth()); notesDataGrid._x = 18;

notesDataGrid._y = 40; notesDataGrid.setSize(225, 170);

notesDataGrid.addColumn(“name”);

notesDataGrid.addColumn(“size”); notesDataGrid.getColumnAt(0).width = 165; notesDataGrid.getColumnAt(0).headerText = “Title”; notesDataGrid.getColumnAt(1).headerText = “Size”;

this.createClassObject(Button, “addButton”, this.getNextHighestDepth()); addButton.label = “Add”;

addButton._x = 18; addButton._y = 219; addButton.setSize(100, 22);

this.createClassObject(Button, “deleteButton”, this.getNextHighestDepth()); deleteButton.label = “Delete”;

deleteButton._x = 142; deleteButton._y = 219; deleteButton.setSize(100, 22);

this.createClassObject(Label, “noteTitle”, this.getNextHighestDepth()); noteTitle.text = “Title”;

noteTitle._x = 272; noteTitle._y = 18;

this.createClassObject(TextInput, “titleField”, this.getNextHighestDepth()); titleField._x = 272;

titleField._y = 40; titleField.setSize(225, 22);

this.createClassObject(Label, “textTitle”, this.getNextHighestDepth()); textTitle.text = “Text”;

textTitle._x = 272; textTitle._y = 88;

this.createClassObject(TextArea, “noteTextField”, this.getNextHighestDepth()); noteTextField._x = 272;

noteTextField._y = 109; noteTextField.setSize(225, 100);

this.createClassObject(Button, “saveButton”, this.getNextHighestDepth()); saveButton.label = “Save”;

713

Appendix A

saveButton._x = 395; saveButton._y = 219; saveButton.setSize(100, 22);

}

drawScreen();

Exercise 2 Solution

import mx.controls.Button; import mx.controls.DataGrid; import mx.controls.Label; import mx.controls.TextInput; import mx.controls.TextArea;

var noteList:Array = new Array();

notesList.push({title:”Note 1”, size:0, text:”The quick brown fox...” }); notesList.push({title:”Note 2”, size:0, text:”The quick brown...” }); notesList.push({title:”Note 3”, size:0, text:”The quick fox...” });

notesList[0].size = notesList[0].text.length; notesList[1].size = notesList[1].text.length; notesList[2].size = notesList[2].text.length;

function drawScreen():Void

{

this.createClassObject(Label, “gridTitle”, this.getNextHighestDepth()); gridTitle.text = “Notes”;

gridTitle._x = 18; gridTitle._y = 18;

this.createClassObject(DataGrid, “notesDataGrid”, this.getNextHighestDepth()); notesDataGrid._x = 18;

notesDataGrid._y = 40; notesDataGrid.setSize(225, 170);

notesDataGrid.addColumn(“title”);

notesDataGrid.addColumn(“size”); notesDataGrid.getColumnAt(0).width = 165; notesDataGrid.getColumnAt(0).headerText = “Title”; notesDataGrid.getColumnAt(1).headerText = “Size”; notesDataGrid.dataProvider = notesList;

this.createClassObject(Button, “addButton”, this.getNextHighestDepth()); addButton.label = “Add”;

addButton._x = 18; addButton._y = 219; addButton.setSize(100, 22);

this.createClassObject(Button, “deleteButton”, this.getNextHighestDepth()); deleteButton.label = “Delete”;

deleteButton._x = 142; deleteButton._y = 219; deleteButton.setSize(100, 22);

this.createClassObject(Label, “noteTitle”, this.getNextHighestDepth());

714

Exercise Answers

noteTitle.text = “Title”; noteTitle._x = 272; noteTitle._y = 18;

this.createClassObject(TextInput, “titleField”, this.getNextHighestDepth()); titleField._x = 272;

titleField._y = 40; titleField.setSize(225, 22);

this.createClassObject(Label, “textTitle”, this.getNextHighestDepth()); textTitle.text = “Text”;

textTitle._x = 272; textTitle._y = 88;

this.createClassObject(TextArea, “noteTextField”, this.getNextHighestDepth()); noteTextField._x = 272;

noteTextField._y = 109; noteTextField.setSize(225, 100); noteTextField.wordWrap = true;

this.createClassObject(Button, “saveButton”, this.getNextHighestDepth()); saveButton.label = “Save”;

saveButton._x = 395; saveButton._y = 219; saveButton.setSize(100, 22);

}

drawScreen();

Chapter 10

Exercise Solution

import mx.controls.Button; import mx.controls.DataGrid; import mx.controls.Label; import mx.controls.TextInput; import mx.controls.TextArea;

var notesList:Array = new Array();

notesList.push({title:”Note 1”, size:0, text:”The quick brown fox...”}); notesList.push({title:”Note 2”, size:0, text:”The quick brown...”}); notesList.push({title:”Note 3”, size:0, text:”The quick fox...”});

notesList[0].size = notesList[0].text.length; notesList[1].size = notesList[1].text.length; notesList[2].size = notesList[2].text.length;

function drawScreen():Void

{

this.createClassObject(Label, “gridTitle”, this.getNextHighestDepth()); gridTitle.text = “Notes”;

gridTitle._x = 18;

715

Appendix A

gridTitle._y = 18;

this.createClassObject(DataGrid, “notesDataGrid”, this.getNextHighestDepth()); notesDataGrid._x = 18;

notesDataGrid._y = 40; notesDataGrid.setSize(225, 170);

notesDataGrid.addColumn(“title”);

notesDataGrid.addColumn(“size”); notesDataGrid.getColumnAt(0).width = 165; notesDataGrid.getColumnAt(0).headerText = “Title”; notesDataGrid.getColumnAt(1).headerText = “Size”; notesDataGrid.dataProvider = notesList;

this.createClassObject(Button, “deleteButton”, this.getNextHighestDepth()); deleteButton.label = “Delete”;

deleteButton._x = 18; deleteButton._y = 219; deleteButton.setSize(100, 22); deleteButton.enabled = false;

this.createClassObject(Label, “noteTitle”, this.getNextHighestDepth()); noteTitle.text = “Title”;

noteTitle._x = 272; noteTitle._y = 18;

this.createClassObject(TextInput, “titleField”, this.getNextHighestDepth()); titleField._x = 272;

titleField._y = 40; titleField.setSize(225, 22);

this.createClassObject(Label, “textTitle”, this.getNextHighestDepth()); textTitle.text = “Text”;

textTitle._x = 272; textTitle._y = 88;

this.createClassObject(TextArea, “noteTextField”, this.getNextHighestDepth()); noteTextField._x = 272;

noteTextField._y = 109; noteTextField.setSize(225, 100); noteTextField.wordWrap = true;

this.createClassObject(Button, “addButton”, this.getNextHighestDepth()); addButton.label = “Add”;

addButton._x = 272; addButton._y = 219; addButton.setSize(100, 22); addButton.enabled = false;

this.createClassObject(Button, “saveButton”, this.getNextHighestDepth()); saveButton.label = “Save”;

saveButton._x = 395; saveButton._y = 219; saveButton.setSize(100, 22); saveButton.enabled = false;

716

Exercise Answers

}

drawScreen();

//Handle the user typing into the title or note fields var handleNoteChangeListener:Object = new Object(); handleNoteChangeListener.change = function()

{

addButton.enabled = true; saveButton.enabled = true;

}

titleField.addEventListener(“change”, handleNoteChangeListener); noteTextField.addEventListener(“change”, handleNoteChangeListener);

//Handle the selecting of a note from the list

var handleNoteListener:Object = new Object(); handleNoteListener.change = function(eventObject:Object)

{

titleField.text = eventObject.target.selectedItem.title; noteTextField.text = eventObject.target.selectedItem.text; deleteButton.enabled = true;

}

notesDataGrid.addEventListener(“change”, handleNoteListener);

// Handle the pressing of the Delete button var handleDeleteListener:Object = new Object(); handleDeleteListener.click = function()

{

trace(“selectedIndex: “ + notesDataGrid.selectedIndex); var selectedRow:Number = notesDataGrid.selectedIndex; notesList.splice(selectedRow, 1); notesDataGrid.dataProvider = notesList;

//Alternately, the following line can replace the previous two

//notesDataGrid.dataProvider.removeItemAt(selectedRow);

if (notesList.length > 0)

{

// The list is not empty

if (selectedRow < notesList.length)

{

// Select the row that takes the deleted row’s place notesDataGrid.selectedIndex = selectedRow;

}

else

{

//There are no more rows to take the selected

//row’s place. Select the last row. notesDataGrid.selectedIndex = notesList.length - 1;

}

notesDataGrid.dispatchEvent({type:”change”, target:notesDataGrid});

}

else

{

// The list is empty. deleteButton.enabled = false;

717