Introduction
One of the most important non-idle game mechanics. Uses standard moving actor code from stencyl.
How it works
Here it shows that your speed per frame cannot be less than 0.5 by using this code. The speed per frame has been shown in the video to be rounded off.
The main discussion is the
Actor.setX(actcor.getX + speed_direction_mult * base_actor_speed);
Actor.setX(actcor.getX + speed_direction_mult * base_actor_speed);
Code:
Requires custom class (free form mode): http://stencyl-nator.blogspot.com/p/text-drawing-arbitrary-code-for-stencyl.html
import com.stencyl.Engine;
import com.stencyl.behavior.ActorScript;
import com.stencyl.behavior.Script;
import com.stencyl.behavior.TimedTask;
import com.stencyl.models.Actor;
import com.stencyl.utils.motion.Easing;
class Player_leg_behav extends ActorScript
{
public var h:Float; public var w:Float; //actor dimensions;
public var x:Float; public var y:Float; //actor positions;
public var diagt:F = new F(); //new drawing method
public var camx:Float; public var camy:Float;
public var move_north:Bool= false; public var move_south:Bool = false;
public var move_east:Bool = false; public var move_west:Bool = false;
public var move_x_mult:Float = 0; public var move_y_mult:Float = 0;
public var move_dir: Int = 0; public var moving:Bool = false;
public var move_rev:Bool = false; public var move_perframe:Float=0.6;
public var chg_frame:Bool = true; public var chg_delay:Int;
public var chg_tt:TimedTask = new TimedTask(null,50,false,null); // unused timed tasks for future uses.
public var frame_id:Int;
public var coll:Bool= false;
public function new (u0:Int,a:Actor,u1:Engine){super(a);}
override public function init()
{
actor.tweenProps.realScaleXY.tween(actor.realScaleX, 0.25, actor.realScaleY, 0.25, Easing.linear,0);
this.x=actor.getX(); this.y = actor.getY();
Engine.engine.cameraFollow(actor);
this.camx = Engine.cameraX; this.camy = Engine.cameraY;
this.diagt.f_cu = Script.getFont(19);
addKeyStateListener("d",press_east); addKeyStateListener("w",press_north); addKeyStateListener("s",press_south); addKeyStateListener("a",press_west);
this.add_def(def); this.add_dra(diagd); this.chg_tt.actor = actor;
}
public function add_def(a:Dynamic):Void{actor.whenUpdatedListeners.push(a);}
public function add_dra(a:Dynamic):Void{actor.whenDrawingListeners.push(a);}
public function calc_frame_time():Void
{
this.chg_delay = 50;
}
public function def():Void
{
if(this.moving)
{
this.x=actor.getX(); this.y = actor.getY();
if(this.chg_frame)
{
this.chg_frame = false;
if(this.move_rev){if(this.frame_id > 0){this.frame_id -= 1;} else{this.move_rev = false;}}
else {if(this.frame_id < 19){this.frame_id += 1;} else{this.move_rev = true;}}
actor.setAnimation("a"+this.frame_id);
Script.runLater(this.chg_delay, enable_change_frame,actor);
}
actor.setX(actor.getX() + this.move_x_mult * this.move_perframe); actor.setY(actor.getY() + this.move_y_mult * this.move_perframe);
}
}
public function diagd():Void
{
this.diagt.ds("moving: "+ this.moving, this.x - this.camx + 60,this.y - this.camy);
this.diagt.ds("change frame: "+ this.chg_frame, this.x - this.camx + 60,this.y - this.camy + 20);
}
public function enable_change_frame(t:TimedTask):Void{this.chg_frame = true;}
public function press_east(p:Bool, r:Bool,u0:Array<Dynamic>):Void
{
if(p)
{ this.moving = true;
this.move_east = true;
if(this.move_north && !(this.move_south)){actor.spinTo(315,0,Easing.linear); this.move_x_mult = 0.707; this.move_y_mult = -0.707;}
else if(!(this.move_north) && this.move_south){actor.spinTo(45,0,Easing.linear); this.move_x_mult = 0.707; this.move_y_mult = 0.707;}
else if(!(this.move_north) && !(this.move_south)){actor.spinTo(0,0,Easing.linear); this.move_x_mult = 1; this.move_y_mult = 0;}
}
else if(r)
{
this.move_east = false;
if(this.move_north && !(this.move_south)){actor.spinTo(270,0,Easing.linear); this.move_x_mult = 0;this.move_y_mult = -1;}
else if(!(this.move_north) && this.move_south){actor.spinTo(90,0,Easing.linear); this.move_x_mult = 0;this.move_y_mult = 1;}
else if(this.move_west) {actor.spinTo(180,0,Easing.linear); this.move_x_mult = -1; this.move_y_mult = 0;};
else {this.move_x_mult = 0;this.move_y_mult = 0; this.moving = false;}
}
}
public function press_north(p:Bool, r:Bool,u0:Array<Dynamic>):Void
{
if(p)
{ this.moving = true;
this.move_north = true;
if(this.move_east && !(this.move_west)){actor.spinTo(315,0,Easing.linear); this.move_x_mult = 0.707;this.move_y_mult = -0.707;}
else if(!(this.move_east) && this.move_west){actor.spinTo(225,0,Easing.linear); this.move_x_mult = -0.707;this.move_y_mult = -0.707;}
else if(!(this.move_east) && !(this.move_west)){actor.spinTo(270,0,Easing.linear); this.move_x_mult = 0;this.move_y_mult = -1;}
}
else if(r)
{
this.move_north = false;
if(this.move_east && !(this.move_west)){actor.spinTo(0,0,Easing.linear); this.move_x_mult = 1;this.move_y_mult = 0;}
else if(!(this.move_east) && this.move_west){actor.spinTo(180,0,Easing.linear); this.move_x_mult = -1;this.move_y_mult = 0;}
else if(this.move_south) {actor.spinTo(90,0,Easing.linear); this.move_x_mult = 0; this.move_y_mult = 1;};
else {this.move_x_mult = 0;this.move_y_mult = 0; this.moving = false;}
}
}
public function press_south(p:Bool, r:Bool,u0:Array<Dynamic>):Void
{
if(p)
{ this.moving = true;
this.move_south = true;
if(this.move_east && !(this.move_west)){actor.spinTo(45,0,Easing.linear); this.move_x_mult = 0.707;this.move_y_mult = 0.707;}
else if(!(this.move_east) && this.move_west){actor.spinTo(135,0,Easing.linear); this.move_x_mult = -0.707;this.move_y_mult = 0.707;}
else if(!(this.move_east) && !(this.move_west)){actor.spinTo(90,0,Easing.linear); this.move_x_mult = 1;this.move_y_mult = 0;}
}
else if(r)
{
this.move_south = false;
if(this.move_east && !(this.move_west)){actor.spinTo(0,0,Easing.linear); this.move_x_mult = 1;this.move_y_mult = 0;}
else if(!(this.move_east) && this.move_west){actor.spinTo(180,0,Easing.linear); this.move_x_mult = -1;this.move_y_mult = 0;}
else if(this.move_north) {actor.spinTo(270,0,Easing.linear); this.move_x_mult = 0; this.move_y_mult = -1;};
else {this.move_x_mult = 0;this.move_y_mult=0; this.moving = false;}
}
}
public function press_west(p:Bool, r:Bool,u0:Array<Dynamic>):Void
{
if(p)
{ this.moving = true;
this.move_west = true;
if(this.move_north && !(this.move_south)){actor.spinTo(225,0,Easing.linear); this.move_x_mult = -0.707;this.move_y_mult = -0.707;}
else if(!(this.move_north) && this.move_south){actor.spinTo(135,0,Easing.linear); this.move_x_mult = -0.707;this.move_y_mult = 0.707;}
else if(!(this.move_north) && !(this.move_south)){actor.spinTo(180,0,Easing.linear); this.move_x_mult = -1;this.move_y_mult = 0;}
}
else if(r)
{ this.moving = true;
this.move_west = false;
if(this.move_north && !(this.move_south)){actor.spinTo(270,0,Easing.linear); this.move_x_mult = 0;this.move_y_mult = -1;}
else if(!(this.move_north) && this.move_south){actor.spinTo(90,0,Easing.linear); this.move_x_mult = 0;this.move_y_mult = 1;}
else if(this.move_east) {actor.spinTo(0,0,Easing.linear); this.move_x_mult = 1; this.move_y_mult = 0;}
else {this.move_x_mult = 0;this.move_y_mult = 0; this.moving = false;}
}
}
}
No comments:
Post a Comment