• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to keep the objects within the stage width & height & what loop should i use?

Community Beginner ,
May 16, 2018 May 16, 2018

Copy link to clipboard

Copied

So i'm trying to make an capture alien game where you will need to use your arrow keys to control a cage to capture the alien. Here is my uncompleted codes

import flash.events.Event;

stop();

lunar_txt.text="Please capture the alien using Up,Down,Left,Right arrow keys!";

var capture:Boolean=false;

var randomX:Number = Math.random() * stage.stageWidth

var randomY:Number = Math.random() * stage.stageHeight

/*alien_mc.addEventListener(Event.ENTER_FRAME, checkcapture)

function checkcapture(event:Event):void

{

while(capture==false)

{

alien_mc.x= randomX;

alien_mc.y= randomY;

}

}

*/

alien_mc.x= randomX;

alien_mc.y= randomY;

var upPressed:Boolean = false;

var downPressed:Boolean = false;

var leftPressed:Boolean = false;

var rightPressed:Boolean = false;

stage.addEventListener(Event.ENTER_FRAME, keydirection);

stage.addEventListener(KeyboardEvent.KEY_DOWN, keycontrols);

stage.addEventListener(KeyboardEvent.KEY_UP, keyboard);

function keydirection(event:Event)

{

if (upPressed)

{

cage_mc.y -= 10;

}

if (downPressed)

{

cage_mc.y += 10;

}

if (leftPressed)

{

cage_mc.x -= 10;

}

if (rightPressed)

{

cage_mc.x += 10;

}

}

function keycontrols(event:KeyboardEvent):void

{

switch (event.keyCode)

{

case Keyboard.UP:

{

upPressed = true;

break;

}

case Keyboard.DOWN:

{

downPressed = true;

break;

}

case Keyboard.LEFT:

{

leftPressed = true;

break;

}

case Keyboard.RIGHT:

{

rightPressed = true;

break;

}

}

}

function keyboard(event:KeyboardEvent):void

{

switch (event.keyCode)

{

case Keyboard.UP:

{

upPressed = false;

break;

}

case Keyboard.DOWN:

{

downPressed = false;

break;

}

case Keyboard.LEFT:

{

leftPressed = false;

break;

}

case Keyboard.RIGHT:

{

rightPressed = false;

break;

}

}

}

alien_mc.addEventListener(Event.ENTER_FRAME, checkinstage)

function checkinstage(event:Event):void

{

if(alien_mc.y > stage.stageHeight)

{

alien_mc.y= randomY;

alien_mc.x= randomX;

}

else if(alien_mc.x > stage.stageWidth)

{

alien_mc.x= randomX;

alien_mc.y= randomY;

}

}

Using while loop for looping the alien to random co-ordinates when it isn't captured gives me this error

"Error: Error #1502: A script has executed for longer than the default timeout period of 15 seconds.

at MainTimeline/checkcapture()"

On the other hand, using do while loop completely crashes my program.

So which loop should i be using in my case? & is my codes wrong? Also, how do i add a delay to the loop? Let's say i want it to change alien position every 2 seconds?

2nd question, is there something wrong with my function checkinstage codes? After adding those codes my alien is still appearing out of the stage

TOPICS
ActionScript

Views

416

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 18, 2018 May 18, 2018

i don't download and correct files unless i'm hired.  free help i only give via the forums.

someone else may download it though.

Votes

Translate

Translate
Community Expert ,
May 16, 2018 May 16, 2018

Copy link to clipboard

Copied

change the 'while' to 'if' in checkcapture and then define your capture boolean

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 16, 2018 May 16, 2018

Copy link to clipboard

Copied

Didn't work ;( after i change it to if, i'm getting more errors from the other scenes, I did declare my capture as a boolean > var capture:Boolean=false;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 17, 2018 May 17, 2018

Copy link to clipboard

Copied

well, it actually did work.  the fact that you have other errors wasn't caused by correcting that error.

it's going to be difficult to correct your code when only part of it's shown.  and if there's a lot more code it's too much to correct in a forum. 

what code did you have. prior to adding your checkcapture function, that worked without error?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 17, 2018 May 17, 2018

Copy link to clipboard

Copied

Actually, the code does work now without showing any error but it didn't loop, it only changes location when i re preview the animation, i want it to keep on looping every 2 seconds within the same animation, here's the file link for you to take a look  hello.fla - Google Drive

if you could help me to fix other errors like the netstream, that would be awesome, also, my alien is still appearing outside of my stage ;(

Located at game 2 btw appreciate your effort in helping me

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2018 May 18, 2018

Copy link to clipboard

Copied

i don't download and correct files unless i'm hired.  free help i only give via the forums.

someone else may download it though.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 19, 2018 May 19, 2018

Copy link to clipboard

Copied

That's the only answer i needed, Thanks for at least taking your time to reply & assist me, i've changed my game plan and ditch this since i couldn't solve it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 19, 2018 May 19, 2018

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines