May 25
Move (1 rész) - AS3
Posted by dody Design | Comments: no responsesWe make a main mover class and in the next stages we could complete it with new things.
First of all let’s have a look how to create classes and why is it good for us to create a class instead of writing the code straight to the frames.
All what you need is Flash CS3 and a bit of programming knowledge (you need minimal to this project).
Creating of a class in AS3 :
1. We must create an ActionScript (.as) file and a Flash File (.fla). The name of the first one should be the name of our class, for example mover.as, you shouldn’t worry about the name of the Flash File, but put all of them in one folder (you could save it to an another folder also - you can see later).
2. You have to type these lines into the mover.as:
package {
public class mover{
public function mover(){
trace(”mover created”);
}
}
}
The mover is the name of our class as you can see in the line “public class”. Our first function is the constructor (that has ran down by creating).
Similarly this will get the name of our class (otherwise you’ll get error message 1180).
3. In the .fla file we call the mover class’s constructor and you get an message (press F2) that “mover created”.
var myMover:mover = new mover();
or you can call this way too:
var myMover:mover
myMover = new mover()
4. Carrying on the class mover you should complete (the code) with the followings:
We should know exactly, what kind of MovieClip would we moving and with what value would be move that.
By the other hand, we could also add a start position.
5. We supplement the file mover.as with this:
package {
import flash.display.MovieClip
public class mover{
public var targetMC:MovieClip
public function mover(targetMC:MovieClip,xStart:Number, yStart:Number){
this.targetMC=targetMC
this.targetMC.x=xStart
this.targetMC.y=yStart
}
public function startMove():void{
trace(this.targetMC.name)
}
}
}
The Constructor has now 3 parameters. The first one is the Movie we will moving, the second one - third one is the start position. We store these ones as the own value of the PELDANY. We establish a public function where we could start the moving.In the FLA create a new symbol (MovieClip) and call it sampleMC instant. Change the frame script to this:
var myMover:mover = new mover(sampleMC, 20, 20);
myMover.stargMove()
or use this way:
var myMover:mover
myMover = new mover(sampleMC, 20, 20);
myMover.startMove()
We get back the name of MC instant, this way we have the first and the last condition.
6. Finally finish our class. Go ahead with file called mover.as:
package {
import flash.display.MovieClip
import flash.events.Event
public class proba{
public var targetMC:MovieClip
public var xMod:Number
public var yMod:Number
public function proba(targetMC:MovieClip,xStart:Number, yStart:Number){
this.targetMC=targetMC
this.targetMC.x=xStart
this.targetMC.y=yStart
}
protected function updatePoz(event:Event):void{
this.targetMC.x+=this.xMod
this.targetMC.y+=this.yMod
}
public function startMove(xMod:Number,yMod:Number):void{
this.xMod=xMod
this.yMod=yMod
this.targetMC.addEventListener(Event.ENTER_FRAME, updatePoz)
}
}
}
In startMove we set the “speed” of the Object (changing of position). After that we add an eventListener what call on every frame the function updatePoz what is increase the x and the y value of the MovieClip with the given “speed”.
In the fla we add these that startMove function get plus value.
var myMover:mover
myMover = new mover(probaMC, 20, 20);
myMover.startMove(1,1)
Thx for help “zaussie”. :)




Post your opinion