May 29

  • magyar

Move Bounce, Visszapattanás (2 rész) – AS3

Posted by dody Design | Comments: 3 responses

In 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

Posted in Flash, Tutorial

Comments

So far, there are 3 Comments | Post your own comment
Avatar:

You can sign up to Gravatar here. If you sign up your e-mail address, your uploaded image will be everywhere where you used it and which site use Gravatar. (One registration, Nothing SPAM and Newsletter!)


  1. No GravatarÉvi said:

    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


  2. No Gravatardody Design said:

    Hát ez egy programozási példa. De köszönöm hogy írtál.
    This’s a programing exemple. Thx for comment.


  3. No GravatarWulpil said:

    Nagyon tanulságos példának tartom!


Post your opinion

About in Brief

Hi there,
I am Dody, the designer and the HTML developer of the website that you are watching right now. I am 27 years old and living in Budapest (Hungary). I am Designer and Interactive application developer (Flash-Java Developer). Currently working as a freelancer. During the spare-time, I study languages and bike. This site (and jozsefgal.dody.hu practice to show what can i make....
[More...]