[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]
[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]
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.
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.
no they should b named block1 block2 etc.
could you show send the fla file and ill have a look.
sure… if you can tell me how