May 29
Move Bounce, Visszapattanás (2 rész) - AS3
Posted by dody Design | Comments: 3 responsesIn this tutorial we will extend the mover class we made previously. The main conditions of the object oriented programming are: Encapsulation (black box, the only important thing is what’s going in and what’s coming out), Polymorphism, Inheritance. The typical example of the last one is moving - bounce - gravitation… the list is endless of the movement’s directions.
package {
import flash.display.MovieClip
import flash.events.Event
public class bound{
public function bound(targetMC:MovieClip,xStart:Number, yStart:Number){
}
}
}
The first part of our code won’t work, because we haven’t declared the variables yet. We don’t have to do that, because we don’t need that in the future. The bounce is a extender class. You can get the class completion with the keyword “extends”. So complete the class mover with the bound.
This could be seems like this:
package {
import flash.display.MovieClip
import flash.events.Event
public class bound extends mover{
public function bound(targetMC:MovieClip,xStart:Number, yStart:Number){
super(targetMC,xStart,yStart)
}
}
}
With the keyword super you can call the parent class. That means, when we will make Object for the class bound then it will do the same operation like the class of the parent, but of course we could also complete it.
With the next few lines we finalise it:
package {
import flash.display.MovieClip
import flash.events.Event
public class bound extends mover{
public function bound(targetMC:MovieClip,xStart:Number, yStart:Number){
super(targetMC,xStart,yStart)
}
override protected function updatePoz(event:Event):void{
bouncertBorder()
super.updatePoz(event)
}
private function bouncertBorder():void{
if((this.targetMC.x>=this.targetMC.stage.stageWidth-this.targetMC.width)||(this.targetMC.x<=0)){
this.xMod *= -1
}
if((this.targetMC.y>=this.targetMC.stage.stageHeight-this.targetMC.height) || (this.targetMC.y<=0)){
this.yMod *= -1
}
}
}
}
With the override keyword can we override parent class. If the parent class has a function called updatePoz() then in the child class we can override it with “override” and we can define again. In our case we do this because we want to complete with the examination of the bounce.
We examine the bounce with a simple if-statement. If it has reached the limit, we have to multiply the value of the movement with minus one what will do opposite of movement.
In the fla the only thing you should do, just type bound instead of mover.
It’s important to define here, what is the difference between the public, private and protected between functions and keywords.
public: Accessible for any of the codes that using the object.
private: It’s available for the objects, in what they are defined.
protected: accessible through subclass (child class) by way of inheritance




Jun 07, 2008 at 9:34 AM
[ ]
szia!
Nem tom, mi ez, de hogy legyen vmi komment, írok most!
I don’t know what is this, but some comment would be here, so i write now.
:-)
Szia: Évi
Jun 07, 2008 at 9:55 AM
[ dody.hu ]
Hát ez egy programozási példa. De köszönöm hogy írtál.
This’s a programing exemple. Thx for comment.
Aug 04, 2009 at 1:50 PM
[ oden.hu ]
Nagyon tanulságos példának tartom!