- 分享
- 0
- 人气
- 0
- 主题
- 20
- 帖子
- 706
- UID
- 19942
- 积分
- 2395
- 阅读权限
- 20
- 注册时间
- 2005-10-28
- 最后登录
- 2021-8-30
- 在线时间
- 643 小时
|
在Flash CS4我有define Land, Arm Hero Movieclip。
Land有很多个Frame,不同的frame代表不同的地形
Arm是Hero的手,Hero走路的时候Arm会摇动。
Hero可以向左向右,也可以跳。
我设定舞台的Main as file 为 LevelMain.as
- package {
- import flash.display.MovieClip;
- import com.level.World;
- import com.level.Config;
- import com.level.Hero;
- import com.level.Control;
- import flash.events.Event;
- import flash.events.KeyboardEvent;
- import flash.ui.Keyboard;
-
- public class LevelMain extends MovieClip {
- var config:Config = new Config();
- var world:World = new World();
- var hero:Hero = new Hero();
- var control:Control = new Control();
-
-
-
-
-
- public function LevelMain() {
- initGame();
- }
-
- public function initGame():void {
-
- config.configWorld(world);
-
- config.configHero(hero);
-
- config.configControl(control);
-
- addChild(world);
- addChild(hero);
-
- this.addEventListener(Event.ENTER_FRAME, gameloop);
- stage.addEventListener(KeyboardEvent.KEY_UP, control.key_up);
- stage.addEventListener(KeyboardEvent.KEY_DOWN, control.key_down);
- }
-
- public function gameloop(e:Event):void {
- heromove();
- }
-
- public function heromove():void {
- var keyNames = control.getKeys();
- if(keyNames.length > 0) {
- for(var i:int; i<keyNames.length; i++) {
- var keyName:String = keyNames[i];
- if(keyName != null) {
- hero.walk(keyName); // direction
- }
- }
- } else {
- hero.stops();
- }
-
-
- }
- }
- }
复制代码
在 folder com/level define 了 5 个 class
Arm.as
- package com.level {
- import flash.display.MovieClip;
-
- public class Arm extends MovieClip {
- var _isSwing:Boolean = false;
-
- public function Arm() {
-
- }
-
- public function startSwing():void {
- this.gotoAndPlay(2);
- _isSwing = true;
- }
-
- public function stopSwing():void {
- this.gotoAndStop(1);
- _isSwing = false;
- }
-
- public function isSwing():Boolean {
- return _isSwing;
- }
- }
- }
复制代码
Config.as
- package com.level {
- import com.level.World;
- import com.level.Land;
- import com.level.Hero;
- import com.level.Control;
- import flash.ui.Keyboard;
- public class Config {
- public function Config() {
-
- }
-
- public function configWorld(world:World):void {
- world.addLand(new Array(1,2,3,2,1,2,1,2,3,1,2,2,3));
- world.init();
- }
-
- public function configHero(hero:Hero):void {
- hero.speed = 5;
- hero.x = 50;
- hero.y = 250;
- }
-
- public function configControl(control:Control):void {
-
- control.addControl("left", Keyboard.LEFT);
- control.addControl("right", Keyboard.RIGHT);
- control.addControl("up", Keyboard.UP);
- control.addControl("down", Keyboard.DOWN);
-
- }
- }
- }
复制代码
Config.as
- package com.level {
- import flash.ui.Keyboard;
- import flash.events.KeyboardEvent;
- public class Control {
- var controls:Array = new Array();
- public function Control() {
-
- }
-
- public function addControl(n:String, key:uint):void {
- controls.push(new Array(n, key, false));
- }
-
- public function key_down(e:KeyboardEvent):void {
-
- for(var i:int; i<controls.length; i++) {
- if(controls[i][1] == e.keyCode) {
- controls[i][2] = true;
- }
- }
- }
-
- public function key_up(e:KeyboardEvent):void {
- for(var i:int; i<controls.length; i++) {
- if(controls[i][1] == e.keyCode) {
- controls[i][2] = false;
- }
- }
- }
-
- public function getKeys():Array {
- var keys = new Array();
- for(var i:int; i<controls.length; i++) {
- if(controls[i][2] == true) {
- keys.push(controls[i][0]);
- }
- }
- return keys;
- }
- }
- }
复制代码
Hero.as
- package com.level {
- import flash.display.MovieClip;
- import flash.events.Event;
- public class Hero extends MovieClip {
- var _speed:int = 5;
-
- var _jump:Boolean = false;
- var _yspeed:int = 0;
- var _gravity:int = 0;
-
- public function Hero() {
-
- this.addEventListener(Event.ENTER_FRAME, updateStatus);
- }
-
- public function updateStatus(e:Event):void {
- this.y += _yspeed;
- _yspeed += _gravity;
- if(this.y > 250) { // hit test here
- this._gravity = 0;
- this.y = 250;
- this._jump = false;
- }
- trace(_yspeed);
- }
-
- public function walk(direction:String):void {
-
-
- if(direction == "right") {
- this.x += _speed;
-
- } else if (direction == "left") {
- this.x -= _speed;
- } else if( direction == "up") {
- if(_jump == false) {
- this.startJump();
- }
- } else if(direction == "down") {
- this.y += speed;
- }
-
-
- if(!this.arm.isSwing()) {
- this.arm.startSwing();
- }
- }
-
- public function stops():void {
- this.arm.stopSwing();
- }
-
- public function startJump():void {
- _jump = true;
- this._yspeed = -50;
- this._gravity = 10;
- }
-
- public function set speed(value:int):void {
- _speed = speed;
- }
-
- public function get speed():int {
- return _speed;
- }
-
- }
- }
复制代码
Land.as
- package com.level {
- import flash.display.MovieClip;
- public class Land extends MovieClip {
- var type:int = 0;
- public function Land(t:int) {
- type = t;
- init();
- }
-
- public function init():void {
- if(type > 0) {
- this.gotoAndStop("land_" + type);
- }
- }
- }
- }
复制代码
World.as
- package com.level {
- import com.level.Land;
- import flash.display.Sprite;
- public class World extends Sprite {
- var lands = new Array();
- public function World() {
-
- }
-
- public function addLand(types:Array):void {
- for(var i:int; i<types.length; i++) {
-
- var land:Land = new Land(types[i]);
- lands.push(land);
- }
- }
-
- public function init():void {
- for(var i:int; i<lands.length; i++) {
- lands[i].x = i * 100;
- lands[i].y = 400;
- addChild(lands[i]);
- }
- }
- }
- }
复制代码 |
|