Okay, now that we have mashed up the two programs, we come to the confusing part. We must fuse them into one cohesive program without error. First, since we are no longer in the jump program - and since we are going to use a true sine function instead of a fake one - there are certain parts of both programs that we no longer need, or must change. These are highlighted in blue:
import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer;
var cirqy:Number = 220
var cirqx:Number = 250
const addam:Number = -1.2
var total:Number = .8
Red.addEventListener(MouseEvent.CLICK, RedJump);
function RedJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, redTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, redTimerDone);
timer.start()
}
function redTimerStep(event:TimerEvent):void {
Green.x -= 8;
}
function redTimerDone(event:TimerEvent):void {
Green.x += 240;
}
function createCircle(event:TimerEvent):void {
var circle:Sprite = new Sprite();
circle.graphics.lineStyle(.5, 1);
circle.graphics.beginFill(0xBC51F8);
circle.graphics.drawCircle(cirqx, cirqy, 5);
graphics.endFill();
addChild (circle);
cirqx = cirqx + addam + 16 ;
total = total + addam;
cirqy = cirqy - (Math.pow(total, 2)+16);
}
We don't need the constant addam or the variable total anymore because we're going to be using the Math.sin() and Math.cos() functions. These functions only receive one parameter, which is the angle.
We're getting rid of the Red event listener because we're not using buttons this time. The timer will simply operate on its own. We will also have to change the names of the timer's functions to reflect the reality of what we're trying to do. Just to make things clearer, we're also going to have to change the timer's intervals from .01 times a thousand milliseconds (a hundredth of a second) to just a hundred milliseconds (a tenth of a second). This was practice was really a holdover from when I learned timers. 100 is just more bug-free than .01 times 1000.
This is a companion blog created by me in order to air some of my discoveries and beginnings since purchasing Adobe Flash CS Professional in April of 2010.
15.3.15
Draw a Circle 2
Now here comes the tricky part. In order to fuze these two programs together, we are going to start by mashing them up in the same actions panel in five sections. The five sections are: Import, Varibales, Timer, Circle and Work. The sections from the Jump program are in red. The sections from the fake sine wave program are in green:
import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer;
var cirqy:Number = 220
var cirqx:Number = 250
const addam:Number = -1.2
var total:Number = .8
Red.addEventListener(MouseEvent.CLICK, RedJump);
function RedJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, redTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, redTimerDone);
timer.start()
}
function redTimerStep(event:TimerEvent):void {
Green.x -= 8;
}
function redTimerDone(event:TimerEvent):void {
Green.x += 240;
}
function createCircle(event:TimerEvent):void {
var circle:Sprite = new Sprite();
circle.graphics.lineStyle(.5, 1);
circle.graphics.beginFill(0xBC51F8);
circle.graphics.drawCircle(cirqx, cirqy, 5);
graphics.endFill();
addChild (circle);
cirqx = cirqx + addam + 16 ;
total = total + addam;
cirqy = cirqy - (Math.pow(total, 2)+16);
}
import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer;
var cirqy:Number = 220
var cirqx:Number = 250
const addam:Number = -1.2
var total:Number = .8
Red.addEventListener(MouseEvent.CLICK, RedJump);
function RedJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, redTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, redTimerDone);
timer.start()
}
function redTimerStep(event:TimerEvent):void {
Green.x -= 8;
}
function redTimerDone(event:TimerEvent):void {
Green.x += 240;
}
function createCircle(event:TimerEvent):void {
var circle:Sprite = new Sprite();
circle.graphics.lineStyle(.5, 1);
circle.graphics.beginFill(0xBC51F8);
circle.graphics.drawCircle(cirqx, cirqy, 5);
graphics.endFill();
addChild (circle);
cirqx = cirqx + addam + 16 ;
total = total + addam;
cirqy = cirqy - (Math.pow(total, 2)+16);
}
Draw a Circle 1
I wanted to animate drawing a circle in Flash using small circles, or points. I got the idea from looking at two other programs that I had created. The first program, called Equation, looks like this:
var cirqy:Number = 220
var cirqx:Number = 250
const addam:Number = -1.2
var total:Number = .8
for (var i:int = 100; i < 107; i++) {
var circle:Sprite = new Sprite();
circle.graphics.lineStyle(.5, 1);
circle.graphics.beginFill(0xBC51F8);
circle.graphics.drawCircle(cirqx, cirqy, 5);
graphics.endFill();
addChild(circle);
cirqx = cirqx + addam + 16 ;
total = total + addam;
cirqy = cirqy - (Math.pow(total, 2)+16);
}
This program was a convoluted way of simulating a sine wave. I probably tried to use the sine function but I didn't know how, which is why I resorted to using adding and exponents. It's passable if you get the numbers right, but it's not a true sine wave.
Here is the second program, which is a re-do of my old Jump program, which was lost forever when my old PC crashed a few years back:
import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer;
Red.addEventListener(MouseEvent.CLICK, RedJump);
function RedJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, redTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, redTimerDone);
timer.start()
}
function redTimerStep(event:TimerEvent):void {
Green.x -= 8;
}
function redTimerDone(event:TimerEvent):void {
Green.x += 240;
}
Blue.addEventListener(MouseEvent.CLICK, BlueJump);
function BlueJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, blueTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, blueTimerDone);
timer.start()
}
function blueTimerStep(event:TimerEvent):void {
Green.x += 8;
}
function blueTimerDone(event:TimerEvent):void {
Green.x -= 240;
}
Yellow.addEventListener(MouseEvent.CLICK, YellowJump);
function YellowJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, yellowTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, yellowTimerDone);
timer.start()
}
function yellowTimerStep(event:TimerEvent):void {
Green.y += 4;
}
function yellowTimerDone(event:TimerEvent):void {
Green.y -= 120;
}
Orange.addEventListener(MouseEvent.CLICK, OrangeJump);
function OrangeJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, orangeTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, orangeTimerDone);
timer.start()
}
function orangeTimerStep(event:TimerEvent):void {
Green.y -= 4;
}
function orangeTimerDone(event:TimerEvent):void {
Green.y += 120;
}
This program will be made through the Actions window, as I am not proficient with classes yet.
var cirqy:Number = 220
var cirqx:Number = 250
const addam:Number = -1.2
var total:Number = .8
for (var i:int = 100; i < 107; i++) {
var circle:Sprite = new Sprite();
circle.graphics.lineStyle(.5, 1);
circle.graphics.beginFill(0xBC51F8);
circle.graphics.drawCircle(cirqx, cirqy, 5);
graphics.endFill();
addChild(circle);
cirqx = cirqx + addam + 16 ;
total = total + addam;
cirqy = cirqy - (Math.pow(total, 2)+16);
}
This program was a convoluted way of simulating a sine wave. I probably tried to use the sine function but I didn't know how, which is why I resorted to using adding and exponents. It's passable if you get the numbers right, but it's not a true sine wave.
Here is the second program, which is a re-do of my old Jump program, which was lost forever when my old PC crashed a few years back:
import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer;
Red.addEventListener(MouseEvent.CLICK, RedJump);
function RedJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, redTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, redTimerDone);
timer.start()
}
function redTimerStep(event:TimerEvent):void {
Green.x -= 8;
}
function redTimerDone(event:TimerEvent):void {
Green.x += 240;
}
Blue.addEventListener(MouseEvent.CLICK, BlueJump);
function BlueJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, blueTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, blueTimerDone);
timer.start()
}
function blueTimerStep(event:TimerEvent):void {
Green.x += 8;
}
function blueTimerDone(event:TimerEvent):void {
Green.x -= 240;
}
Yellow.addEventListener(MouseEvent.CLICK, YellowJump);
function YellowJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, yellowTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, yellowTimerDone);
timer.start()
}
function yellowTimerStep(event:TimerEvent):void {
Green.y += 4;
}
function yellowTimerDone(event:TimerEvent):void {
Green.y -= 120;
}
Orange.addEventListener(MouseEvent.CLICK, OrangeJump);
function OrangeJump(event:MouseEvent):void
{
timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, orangeTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, orangeTimerDone);
timer.start()
}
function orangeTimerStep(event:TimerEvent):void {
Green.y -= 4;
}
function orangeTimerDone(event:TimerEvent):void {
Green.y += 120;
}
This program will be made through the Actions window, as I am not proficient with classes yet.
Welcome back!
Hello, everyone. I know that it has been almost four years since my last post. I don't know why I went away for so long. I think I was just frustrated because everything I tried to post wasn't working. I gave up because I got tired of posting stuff that didn't work. That gave me an idea. From now on, I will only post stuff if I get it to work first. And from now on, I'm going to be working mainly from tutorials. This way, I can start off with code that I know works, and then move on to modify it to make it more interesting. I have more than one book that is full of tutorials on how to make a game, so I will be uploading my versions of those tutorials as well. For now, I have just made a program that works and I would like to show it to you.
17.5.11
Okay. Here is the code that didn't work last time.
package {
import flash.display.MovieClip;
import flash.sensors.Accelerometer;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.ui.KeyLocation;
import flash.events.KeyboardEvent;
public class PlaneGame extends MovieClip {
public var level:Level1;
public var player:Plane;
public function PlaneGame() {
creategame();
createplayer();
addlisteners();
}
private function creategame():void
{
var level:Level1 = new Level1() ;
level.x=level.y=-0
addChild(level);
}
private function createplayer():void
{
var player:Plane = new Plane();
player.x=player.y = 100;
addChild(player);
}
private function addlisteners():void
{
stage.addEventListener(Event.ENTER_FRAME, movelevel, false, 0, false);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveplayer, false, 1, false);
}
private function movelevel(e:Event):void
{
level.x-=10
}
private function moveplayer(k:KeyboardEvent):void
{
switch (k.keyCode)
{
case Keyboard.UP:
{
player.y -= 5;
break;
}
case Keyboard.DOWN:
{
player.y += 5;
break;
}
case Keyboard.LEFT:
{
player.x -= 5;
break;
}
case Keyboard.RIGHT:
{
player.x += 5;
break;
}
}
}
}
}
package {
import flash.display.MovieClip;
import flash.sensors.Accelerometer;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.ui.KeyLocation;
import flash.events.KeyboardEvent;
public class PlaneGame extends MovieClip {
public var level:Level1;
public var player:Plane;
public function PlaneGame() {
creategame();
createplayer();
addlisteners();
}
private function creategame():void
{
var level:Level1 = new Level1() ;
level.x=level.y=-0
addChild(level);
}
private function createplayer():void
{
var player:Plane = new Plane();
player.x=player.y = 100;
addChild(player);
}
private function addlisteners():void
{
stage.addEventListener(Event.ENTER_FRAME, movelevel, false, 0, false);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveplayer, false, 1, false);
}
private function movelevel(e:Event):void
{
level.x-=10
}
private function moveplayer(k:KeyboardEvent):void
{
switch (k.keyCode)
{
case Keyboard.UP:
{
player.y -= 5;
break;
}
case Keyboard.DOWN:
{
player.y += 5;
break;
}
case Keyboard.LEFT:
{
player.x -= 5;
break;
}
case Keyboard.RIGHT:
{
player.x += 5;
break;
}
}
}
}
}
I'm going to remove the function "add listeners" from the constructor, add those listeners to the creategame and createplayer functions and then lead them to the resultant functions.
6.5.11
Game Doesn't Work!
As was written in my previous post, my Game didn't work. There were two compiler errors:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
From just looking at these two errors, I have discerned that the cause of the problem is the "null object reference", which I assume means the the Actionscript file is calling objects that were not even instantiated yet or added to the Display list- one or the other.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlaneGame/movelevel()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlaneGame/movelevel()
Well, looking back at the code, I can see that the main variables in the game- na aircraft and na stage- have already been instantiated, and therefore the problem must be that the objects were not added to the display list before they were worked on. So that's my answer.
First Game OMG!
Yes well I tried to make a game a week ago. Here is the code:
package {
import flash.display.MovieClip;
import flash.sensors.Accelerometer;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.ui.KeyLocation;
import flash.events.KeyboardEvent;
public class PlaneGame extends MovieClip {
public var level:Level1;
public var player:Plane;
public function PlaneGame() {
creategame();
createplayer();
addlisteners();
}
private function creategame():void
{
var level:Level1 = new Level1() ;
level.x=level.y=-0
addChild(level);
}
private function createplayer():void
{
var player:Plane = new Plane();
player.x=player.y = 100;
addChild(player);
}
private function addlisteners():void
{
stage.addEventListener(Event.ENTER_FRAME, movelevel, false, 0, false);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveplayer, false, 1, false);
}
private function movelevel(e:Event):void
{
level.x-=10
}
private function moveplayer(k:KeyboardEvent):void
{
switch (k.keyCode)
{
case Keyboard.UP:
{
player.y -= 5;
break;
}
case Keyboard.DOWN:
{
player.y += 5;
break;
}
case Keyboard.LEFT:
{
player.x -= 5;
break;
}
case Keyboard.RIGHT:
{
player.x += 5;
break;
}
}
}
}
}
package {
import flash.display.MovieClip;
import flash.sensors.Accelerometer;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.ui.KeyLocation;
import flash.events.KeyboardEvent;
public class PlaneGame extends MovieClip {
public var level:Level1;
public var player:Plane;
public function PlaneGame() {
creategame();
createplayer();
addlisteners();
}
private function creategame():void
{
var level:Level1 = new Level1() ;
level.x=level.y=-0
addChild(level);
}
private function createplayer():void
{
var player:Plane = new Plane();
player.x=player.y = 100;
addChild(player);
}
private function addlisteners():void
{
stage.addEventListener(Event.ENTER_FRAME, movelevel, false, 0, false);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveplayer, false, 1, false);
}
private function movelevel(e:Event):void
{
level.x-=10
}
private function moveplayer(k:KeyboardEvent):void
{
switch (k.keyCode)
{
case Keyboard.UP:
{
player.y -= 5;
break;
}
case Keyboard.DOWN:
{
player.y += 5;
break;
}
case Keyboard.LEFT:
{
player.x -= 5;
break;
}
case Keyboard.RIGHT:
{
player.x += 5;
break;
}
}
}
}
}
Not surprisingly, the code didn't work. I got two compiler errors. These are the errors:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlaneGame/movelevel()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlaneGame/movelevel()
What do I want to do with this, huh?
Subscribe to:
Posts (Atom)