# Need to clean up IF statements - \[Flash MX 2004 - AS\]

**URL:** https://forum.kirupa.com/t/need-to-clean-up-if-statements-flash-mx-2004-as/186686
**Category:** flash
**Created:** [April 23, 2006, 6:03pm UTC](https://forum.kirupa.com/t/need-to-clean-up-if-statements-flash-mx-2004-as/186686 "2006-04-23T18:03:41Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![GormDraig](https://avatars.discourse-cdn.com/v4/letter/g/ed8c4c/32.png) [@GormDraig](https://forum.kirupa.com/u/GormDraig)
#### Post date: [April 23, 2006, 6:03pm UTC](https://forum.kirupa.com/t/need-to-clean-up-if-statements-flash-mx-2004-as/186686/1 "2006-04-23T18:03:41Z")

</div>

[SIZE=1]Right heres the **plan** , I’m going to **post** on this message the **code** I have used for a game I’m making, then some **person** with more **knowledge** in **ActionScript** /any code really can maybe **clean** it up a little.[/SIZE]

[SIZE=1]Its a load of **IF** statements and I’m sure it can be done better:[/SIZE]

```auto

[SIZE=1][SIZE=2]onClipEvent (keyDown) {[/SIZE]
 if (Key.isDown(Key.RIGHT)) {
  this._x = this._x+10;
 }
 if (Key.isDown(Key.LEFT)) {
  this._x = this._x-10;
 }
 if (Key.isDown(Key.DOWN)) {
  this._y = this._y+10;
 }
 if (Key.isDown(Key.UP)) {
  this._y = this._y-10;
 }
}
onClipEvent (enterFrame) {
 if (this, hitTest(_root.block1)) {
  this._x = 690;
 }
 if (this, hitTest(_root.block2)) {
  this._y = 173;
 }
 if (this, hitTest(_root.block3)) {
  this._y = 173;
 }
 if (this, hitTest(_root.block4)) {
  this._x = 470;
 }
 if (this, hitTest(_root.block5)) {
  this._x = 235;
 }
 if (this, hitTest(_root.block6)) {
  this._y = 55;
 }
 if (this, hitTest(_root.block7)) {
  this._x = 15;
 }
 if (this, hitTest(_root.block8)) {
  this._y = 260;
 }
}
[/SIZE]

```

[SIZE=1]  
[/SIZE]

[SIZE=1]And also [if you are **kind** enough] please **advise** me on how to make **motion** smoother, as the way I have in the **code** makes it jump 10px at a time, which is the **speed** I want, but there must be **a way** of making it smoother than jumping 10px all the time.[/SIZE]

[SIZE=1]All **help** shall be **appreciated**.[/SIZE]

---

<div class="post-metadata">

### Author: ![Macaroni\_Ted](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@Macaroni\_Ted](https://forum.kirupa.com/u/Macaroni_Ted)
#### Post date: [April 24, 2006, 2:47am UTC](https://forum.kirupa.com/t/need-to-clean-up-if-statements-flash-mx-2004-as/186686/2 "2006-04-24T02:47:23Z")

</div>

right well first remove the onClipEvent (keyDown) event handler and put all this code into the onClipEvent (enterFrame) handler. and i would use switch case instead of lots of if statements.

then with the hit test you can use this simple code to dynamically check all the blocks  
[AS] for(var i=1;i\<=8;i++){  
if(this.hitTest(\_root[“block”+i])){  
//do something  
}  
}[/AS]

the only problem is you have all seperate coordinates for each block and sometimes itll affect the \_x and sometimes the \_y so the easiest way is to put this code on every block  
[AS]onClipEvent(load){  
//this is an example for block1  
this.moveX= 690  
this.moveY=\_root.yourMC.\_y  
// yourMC= whatever you have named the mc you are moving  
// if you ony wanted to move the y not the x then just change it round  
}[/AS]

and so finally you can have thii code in the hitTest if statement

[AS] if(this.hitTest(\_root[“block”+i])){  
this.\_x=\_root[“block”+i].moveX;  
this.\_x=\_root[“block”+i].moveY;  
}[/AS]

so the cod on the MC you want to move is:

[AS]onClipEvent (enterFrame) {  
switch (true) {  
case Key.isDown(Key.RIGHT) :  
this.\_x+=10;  
break;  
case Key.isDown(Key.LEFT) :  
this.\_x-=10;  
break;  
case Key.isDown(Key.DOWN) :  
this.\_y +=10;  
break;  
case Key.isDown(Key.UP) :  
this.\_y -=10;  
break;  
}  
for(var i=1;i\<=8;i++){  
if(this.hitTest(\_root[“block”+i])){  
this.\_x=\_root[“block”+i].moveX  
this.\_x=\_root[“block”+i].moveY  
}  
}  
}[/AS]

the only problem is all the individual coordinates. there are other ways to achieve this but i dont know what you want to do.

with the smoothness of movement it sould be better now. if not then play around with the frame rate and speed untin you get what you want.

---

<div class="post-metadata">

### Author: ![GormDraig](https://avatars.discourse-cdn.com/v4/letter/g/ed8c4c/32.png) [@GormDraig](https://forum.kirupa.com/u/GormDraig)
#### Post date: [April 24, 2006, 8:20am UTC](https://forum.kirupa.com/t/need-to-clean-up-if-statements-flash-mx-2004-as/186686/3 "2006-04-24T08:20:46Z")

</div>

Right I tried that and it runs much smoother than before, but the blocks aren’t stopping the character, I have named my blocks:

block1  
block2  
block3  
etc.

Could that be the problem? Should I name the block MC’s as “block” for each of them to make the hitTest you advised work?

But apart from that its looking good.

---

<div class="post-metadata">

### Author: ![Macaroni\_Ted](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@Macaroni\_Ted](https://forum.kirupa.com/u/Macaroni_Ted)
#### Post date: [April 24, 2006, 8:26am UTC](https://forum.kirupa.com/t/need-to-clean-up-if-statements-flash-mx-2004-as/186686/4 "2006-04-24T08:26:01Z")

</div>

no they should b named block1 block2 etc.  
could you show send the fla file and ill have a look.

---

<div class="post-metadata">

### Author: ![GormDraig](https://avatars.discourse-cdn.com/v4/letter/g/ed8c4c/32.png) [@GormDraig](https://forum.kirupa.com/u/GormDraig)
#### Post date: [April 24, 2006, 8:28am UTC](https://forum.kirupa.com/t/need-to-clean-up-if-statements-flash-mx-2004-as/186686/5 "2006-04-24T08:28:42Z")

</div>

sure… if you can tell me how

---

<div class="post-metadata">

### Author: ![Macaroni\_Ted](https://avatars.discourse-cdn.com/v4/letter/m/df788c/32.png) [@Macaroni\_Ted](https://forum.kirupa.com/u/Macaroni_Ted)
#### Post date: [April 24, 2006, 8:31am UTC](https://forum.kirupa.com/t/need-to-clean-up-if-statements-flash-mx-2004-as/186686/6 "2006-04-24T08:31:15Z")

</div>

ill pm you my Email
