Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Davidflynn · Feb 08, 2012 at 04:13 PM · movementflash

Flash Prevent Digonal Movement

I am trying to design my first flash game in unity I am have my player moving around but i don't want it to be able to move diagonally and my code is allowing it.

 package  
 {
     import flash.display.*;
     import flash.events.*;
     import fl.motion.easing.Bounce;
     
     
     public class Game extends MovieClip
     {
         //variables
         private var player:Player; //code ref movie
         private var speed:int; // referenc to speed
         
         private var left:Boolean; // controles left
         private var right:Boolean; // controles right
         private var up:Boolean; // controles up
         private var down:Boolean; // controles down
 
         public function Game():void 
         {
             // focus on the stage
             stage.focus = stage;
             
             // listen for keyboard events
             stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownFunction);// looking for key to be pressed down
             stage.addEventListener(KeyboardEvent.KEY_UP,keyUpFunction);// looking  for key to be up
             
             // creat the player
             player = new Player(); // creates the new player
             player.x = 300; // movement limits
             player.y = 50; // movement limits
             addChild(player);
             
             // set the speed
             speed = 10;
             
             // main game loop
             stage.addEventListener(Event.ENTER_FRAME, gameLoop);
             
             
         }
         // game loop
         public function gameLoop(event:Event)
         
         {
             //keep the player within the bounds
             if(left == true)
             
             {
                 if(player.x>20)
                 {
                     player.x = player.x - speed;
                 }
                 else if (player.x <20)player.x = 20;
             }
             if(right == true)
             {
                 if(player.x < 580)
                 {
                     player.x = player.x + speed;
                 }
                 else if (player.x > 580) player.x = 580;
             }
             if(down == true)
             {
                 if(player.y < 380)
                 {
                     player.y = player.y + speed;
                 }
                 else if(player.y > 380) player.y = 380;
             }
             if(up == true)
             {
                 if(player.y> 20)
                 {
                     player.y = player.y - speed;
                 }
                 else if(player.y < 20) player.y = 20;
             }
         }
         
         
         //key pressed
         public function keyDownFunction(event:KeyboardEvent) // check if key is down
         
         {
             // go left
             if(event.keyCode == 65) //looking for it to be pressed key code to use that key
             {
                 left = true; // go left
             }
             // go right
             if(event.keyCode == 68)//looking for it to be pressed key code to use that key
             {
                 right = true; // go right
             }
             // go up
             if(event.keyCode == 87)//looking for it to be pressed key code to use that key
             {
                 up = true; // go up
             }
             //go down
             if(event.keyCode == 83)//looking for it to be pressed key code to use that key
             {
                 down = true; //  go down
             }
         }
         //key lifted
         public function keyUpFunction(event:KeyboardEvent) // check to see if he has been released
         
         {
             
             if(event.keyCode == 65)//looking for it to be released key code to use that key
             {
                 left = false; // dont go left
             }
             
             if(event.keyCode == 68)//looking for it to be released key code to use that key
             {
                 right = false; // dont go right
             }
             
             if(event.keyCode == 87)//looking for it to be released key code to use that key
             {
                 up = false; // dont go up
             }
             
             if(event.keyCode == 83)//looking for it to be released key code to use that key
             {
                 down = false; // dont go down
             }
         }
 
     }
     
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Loius · Feb 08, 2012 at 05:24 PM

Consider working through more tutorials - this is very basic.

Don't set up or down to true unless left and right are false. (or vice-versa).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Curvy Spline Tool (Controlling Speed) 2 Answers

Making player move in an arc 0 Answers

Adding simple strafing to my character controller script... 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges