ActionScript 3.0 Game Programming
Chapter two on page 54
Submitted by proto.x on Thu, 03/18/2010 - 11:49.Greetings everybody
I am working through the book
to learn more.
Unfortanally i got stuck on page 54
I supose to make an event lissener
towards BOTH cubes you made with just
pure programming. As you can read on
that page the lissener seeks for wether
you click with your mouse either on one
of those cubes and if so then place the
third sprite you made (the cirkel) on
100,100 (center) of that cube.
Everything worked fine except for when i
adding the lissener it wont work.
It works adding all three items on the right
possition yet when i add the five line's i get
an error. Could anybody help me?
*error* Type was not found or was not a compile-time constant:
MouseEvent function clickSprite(event:MouseEvent){
PS: i already checked wether the name of the command is the same
(in case of the book its called clickSprite)
and i already checked wether the name of the sprite you want to
give a lissener is correct (in the case of the book its called
sprite1 / sprite2 :::: wich you give a lissener)
Also i re-readed everything yet it just keeps whining with the
same error
Also if this helps YES the programming was made in an AS file wich is
linked to a FLA file with the same name. (wich you learn when you start
with the hello world tradition)
Furtermore here is my programming
(I dont mind if you copy it to try it out since its a practice from a book so
all the credits go's towards the writer and maker of the book
Also to make it more clearly whats wrong if you remove the text wich is noted again
all the way below you'll notice if you run it again it works perfectly)
package {
import flash.display.*;
import flash.text.*;
public class BookLessonMultipleSprites2 extends MovieClip {
public function BookLessonMultipleSprites2() {
//Sprite 1 Cube 01
var sprite1:Sprite = new Sprite();
sprite1.graphics.lineStyle(2,0x000000);
sprite1.graphics.beginFill(0xcccccc);
sprite1.graphics.drawRect(0,0,200,200);
sprite1.x = 50;
sprite1.y = 50;
addChild(sprite1);
//Sprite 1 Cube 01
//Sprite 2 Cube 02
var sprite2:Sprite = new Sprite();
sprite2.graphics.lineStyle(2,0x000000);
sprite2.graphics.beginFill(0xcccccc);
sprite2.graphics.drawRect(0,0,200,200);
sprite2.x = 300;
sprite2.y = 50;
addChild(sprite2);
//Sprite 2 Cube 02
//Sprite 3 Cirkel 01
var sprite3:Sprite = new Sprite();
sprite3.graphics.lineStyle(2,0x000000);
sprite3.graphics.beginFill(0x333333);
sprite3.graphics.drawCircle(0,0,25);
sprite3.x = 100;
sprite3.y = 100;
addChild(sprite3);
//Sprite 3 Cirkel 01
sprite1.addEventListener(MouseEvent.CLICK, clickSprite);
sprite2.addEventListener(MouseEvent.CLICK, clickSprite);
function clickSprite(event:MouseEvent){
event.currentTarget.addChild(sprite3);
}
}}}
This is the text that gives the error
sprite1.addEventListener(MouseEvent.CLICK, clickSprite);
sprite2.addEventListener(MouseEvent.CLICK, clickSprite);
function clickSprite(event:MouseEvent){
event.currentTarget.addChild(sprite3);
}
If you DO CUT this part out and run it agian you'll notice it works more then fine
Thank you in progress
Error
Submitted by lemonade295 on Fri, 03/12/2010 - 22:11.What does this mean?
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Enball/endie()
at Chaos_fla::MainTimeline/checkhits()
at Chaos_fla::MainTimeline/moveobs()
KEY_UP, KEY_DOWN Woes
Submitted by aetharr on Sun, 03/07/2010 - 16:56.Ok, here is my problem. Using the system you illustrated in your book for keyboard movement, I have a problem which I just cannot seem to figure out. Here is the code for movement, which you'll have seen a million times already :P
public var leftArrow,rightArrow,downArrow,upArrow:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunction);
public function keyDownFunction(event:KeyboardEvent) {
if (event.keyCode == 37 || event.keyCode == 65) {
leftArrow = true;
} else if (event.keyCode == 39 || event.keyCode == 68) {
rightArrow = true;
} else if (event.keyCode == 38 || event.keyCode == 87) {
upArrow = true;
} else if (event.keyCode == 40 || event.keyCode == 83) {
downArrow = true;
}
}
public function keyUpFunction(event:KeyboardEvent) {
if (event.keyCode == 37 || event.keyCode == 65) {
leftArrow = false;
} else if (event.keyCode == 39 || event.keyCode == 68) {
rightArrow = false;
} else if (event.keyCode == 38 || event.keyCode == 87) {
upArrow = false;
} else if (event.keyCode == 40 || event.keyCode == 83) {
downArrow = false;
}
}
Ok, so that's the code I use, everything else, acts on if the boolean variables are set to true or false, like in your example. The only modification to the code is the extra keyCode which enables the user to use UP,DOWN,LEFT,RIGHT And or, WASD too. However, that is not final, and the finished product will likely have the ability to change these keys... Anyway.
My problem is this:
The system is awesome, unless.. If WHILE HOLDING a key, the user right-clicks, or sometimes it happens on it's own, perhaps if the keyboard thinks you're pressing too many keys. ( It would usually happen too if the window lost focus, but I have a function in place to automatically pause the game and set all the movement variables to false in this case, so it does not matter. ) The game doesn't register that the user has released a key, and because of this, the variable remains true, while the user ISNT holding the key down. The only way to counter this is to have the window lose focus and it resets all of the variables to false, Or to have the user press the button corresponding to the True variable, and then release it.
Is there a way I can prevent this from happening? I would really appreciate your assistance.
Thanks for taking to time to read,
Regards
Pete
XML level editor
Submitted by jdsantiagojr on Fri, 03/05/2010 - 17:30.I am trying to find some information on making levels in my game using XML. Can any one point me in the right direction. I would like to create 10 levels but with out all the class files. is there away to use mainclass.as and load stage one stage two etc.?
I have separate class files for my characters and enemies but would like to use xml, for stages. How do I?
help me please ..to make rapid press
Submitted by kiroszz on Wed, 03/03/2010 - 14:52.I have a problem making character attack with AS3 ,.. when I try programing one press keyboard (key space) to attack, it will happen continoussly when the key holding down press... I just need one tab press then the attack excute,.. not holding press .. help me please.... I beg U all,.. btw sorry for bad english ... here my source code:
package
{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
public class Main extends MovieClip
{
var vx:int;
var vy:int;
var serang:Boolean=true;
public function Main()
{
init();
}
function init():void
{
vx=0;
vy=0;
serang=false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyTekan);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyLepas);
stage.addEventListener(Event.ENTER_FRAME, onEnterframe);
}
function onKeyTekan(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{
if (serang == true)
{
Player.gotoAndStop(3);
serang == false;
}
}
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
Player.gotoAndStop(2);
Player.scaleX = -0.3;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
Player.gotoAndStop(2);
Player.scaleX = 0.3;
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
Player.gotoAndStop(2);
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5;
Player.gotoAndStop(2);
}
}
function onKeyLepas(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
{
vx = 0;
Player.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
{
vy = 0;
Player.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.SPACE)
{
Player.gotoAndStop(1);
serang=true;
}
}
function onEnterframe(event:Event):void
{
Player.x+=vx;
Player.y+=vy;
}
}
}
1137
Submitted by ingcha on Tue, 03/02/2010 - 14:54.I got error on this, can anyone tell me what's wrong? n how to fix it? thanks.
1137: Incorrect number of arguments. Expected no more than 1.
{public function moveMeats(timeDiff:int)
{
for(var j:int=0;j
Found great article on Adobe.com
Submitted by mightybotme on Mon, 03/01/2010 - 17:19.I found an article that talks about an introduction to developing games on the Adobe Flash Platform. It really helped me out:
http://www.adobe.com/newsletters/edge/october2009/articles/article1/inde...
Issues with scrolling
Submitted by windwarp on Sat, 02/27/2010 - 00:16.Hello, I'm looking for some assistance with some side scrolling issues I'm having with my code. This is my first time really coding so any and all help/advice would be a big help! In total I have 3 timelines running, one for pure Action script, one for my movie clips, and a final for my background(this line I have not started to work with, and am not currently concerned about).
So here is my code(its a bit messy, please forgive me):
~~~~~~~
hero.gotoAndStop('still');
var friction:Number=.9;
var speed:Number=0;
var dy:Number=0;
var gravity:Number=4;
var canjump:Boolean = false;
var Key:KeyObject = new KeyObject(stage);
stage.addEventListener(Event.ENTER_FRAME,onenter);
scrollStage();
function scrollStage():void
{
ground.x += (stage.stageWidth * .5) + hero.x;
hero.x =(stage.stageWidth * 0.5);
}
function onenter(e:Event):void{
dy+=gravity;
if(hero.y>300){
canjump=true;
}
if(Key.isDown(Key.UP) && canjump){
dy=-25;
canjump=false;
}
hero.y+=dy;
if (Key.isDown(Key.RIGHT)){
speed-=6
hero.x+=12;
hero.scaleX=1;
hero.gotoAndStop('walking');
}else if (Key.isDown(Key.LEFT)){
speed+=6
hero.x+=-12;
hero.scaleX=-1;
hero.gotoAndStop('walking');
}else {
hero.gotoAndStop('still');
}
if (speed>20){
speed=20;
}
if (speed<(-20)) {
speed=-20;
}
speed=speed*friction;
hero.x-=speed;
if (Math.abs(speed)<0.0009);{
if (! ground.hitTestPoint(hero.x,hero.y,true)) {
hero.y+=dy;
canjump=false;
}
if (dy>10) {
dy=10;
}
for (var i:int = 0; i<10; i++) {
if (ground.hitTestPoint(hero.x,hero.y,true)) {
hero.y--;
dy=0;
canjump=true;
}
}
function scrollStage():void
{
ground.x += (stage.stageWidth * 1) - hero.x;
hero.x = stage.stageWidth * 1;
}
}
}
~~~~~~~~~~~~~~~~
Again any advice on my situation would be greatly appreciated.
Thanks
-Windwarp
Match Three - level Increase, bonus Points
Submitted by rayhtown on Thu, 02/25/2010 - 20:33.Any suggestions for adding levels increase when a score is met and bonus points on the next level on on game pieces.
Jigsaw Game: How to create realistic puzzle pieces?
Submitted by rickramble on Mon, 02/22/2010 - 18:32.Gary's book adds a tantalizing note on the subject of making "puzzle pieces look like traditional puzzle pieces with interlocking tabs. This cosmetic feature can be done in Flash, but only by using quite a bit of vector drawing and bitmap-manipulation tools...".
Having mastered the fundamentals, I'm interested in doing just that. Are there any resources available to show the way?
Copyright Gary Rosenzweig
