Highlighted

I am a beginner in the Actionscript language. How do I resolve Syntax error: package is unexpected?

New Here ,
Jun 29, 2019

Copy link to clipboard

Copied

When I click CTRL + ENTER there is always the error: Syntax error: package is unexpected. How do I fix this error ??

I leave my code if anyone wants to help, thanks!

stop();

package {

import flash.display.MovieClip;

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

import flash.events.Event;

public class Main extends MovieClip {

const gravity: Number = 1.5;

const dist_btw_obstacles: Number = 300;

const ob_speed: Number = 8;

const jump_force: Number = 15;

var player: Player = new Player();

var lastob: Obstacle = new Obstacle();

var obstacles: Array = new Array();

var yspeed: Number = 0;

var score: Number = 0;

public function Main() {

init();

}

function init(): void {

player = new Player();

lastob = new Obstacle();

obstacles = new Array();

yspeed = 0;

score = 0;

player.x = stage.stageWidth / 2;

player.y = stage.stageHeight / 2;

addChild(player);

createObstacle();

createObstacle();

createObstacle();

addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);

stage.addEventListener(KeyboardEvent.KEY_UP, key_up);

}

private function key_up(event: KeyboardEvent) {

if (event.keyCode == Keyboard.SPACE) {

yspeed = -jump_force;

}

}

function restart() {

if (contains(player))

removeChild(player);

for (var i: int = 0; i < obstacles.length; ++i) {

if (contains(obstacles) && obstacles != null)

removeChild(obstacles);

obstacles = null;

}

obstacles.slice(0);

init();

}

function onEnterFrameHandler(event: Event) {

//update player

yspeed += gravity;

player.y += yspeed;

if (player.y + player.height / 2 > stage.stageHeight) {

restart();

}

if (player.y - player.height / 2 < 0) {

player.y = player.height / 2;

}

for (var i: int = 0; i < obstacles.length; ++i) {

updateObstacle(i);

}

scoretxt.text = String(score);

}

function updateObstacle(i: int) {

var ob: Obstacle = obstacles;

if (ob == null)

return;

ob.x -= ob_speed;

if (ob.x < -ob.width) {

changeObstacle(ob);

}

if (ob.hitTestPoint(player.x + player.width / 2, player.y + player.height / 2, true) || ob.hitTestPoint(player.x + player.width / 2, player.y - player.height / 2, true) || ob.hitTestPoint(player.x - player.width / 2, player.y + player.height / 2, true) || ob.hitTestPoint(player.x - player.width / 2, player.y - player.height / 2, true)) {

restart();

}

if ((player.x - player.width / 2 > ob.x + ob.width / 2) && !ob.covered) {

++score;

ob.covered = true;

}

}

function changeObstacle(ob: Obstacle) {

ob.x = lastob.x + dist_btw_obstacles;

ob.y = 100 + Math.random() * (stage.stageHeight - 200);

lastob = ob;

ob.covered = false;

}

function createObstacle() {

var ob: Obstacle = new Obstacle();

if (lastob.x == 0)

ob.x = 800;

else

ob.x = lastob.x + dist_btw_obstacles;

ob.y = 100 + Math.random() * (stage.stageHeight - 200);

addChild(ob);

obstacles.push(ob);

lastob = ob;

}

}

}

ActionScript 3

Adobe Community Professional
Correct answer by Colin Holgate | Adobe Community Professional

Anything that can be done with classes can be done in the timeline. Mainly you need to remove anything that talks about package or private or public. The example code also uses other classes, which could be movieclips in the library. Inside those movieclips there could also be timeline code, though in your flappy birds test I don't think there is.

Here is how the timeline code would look, and it does work if you have a Player movieclip and Obstacle movieclip in the library, and a scoretxt dynamic textfield on the stage. In general, following tutorials that have a download of the files would help.

init();

stop();

import flash.display.MovieClip;

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

import flash.events.Event;

const gravity: Number = 1.5;

const dist_btw_obstacles: Number = 300;

const ob_speed: Number = 8;

const jump_force: Number = 15;

var player: Player = new Player();

var lastob: Obstacle = new Obstacle();

var obstacles: Array = new Array();

var yspeed: Number = 0;

var score: Number = 0;

function init(): void {

  player = new Player();

  lastob = new Obstacle();

  obstacles = new Array();

  yspeed = 0;

  score = 0;

  player.x = stage.stageWidth / 2;

  player.y = stage.stageHeight / 2;

  addChild(player);

  createObstacle();

  createObstacle();

  createObstacle();

  addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);

  stage.addEventListener(KeyboardEvent.KEY_UP, key_up);

}

function key_up(event: KeyboardEvent) {

  if (event.keyCode == Keyboard.SPACE) {

  yspeed = -jump_force;

  }

}

function restart() {

  if (contains(player))

  removeChild(player);

  for (var i: int = 0; i < obstacles.length; ++i) {

  if (contains(obstacles) && obstacles != null)

  removeChild(obstacles);

  obstacles = null;

  }

  obstacles.slice(0);

  init();

}

function onEnterFrameHandler(event: Event) {

  //update player

  yspeed += gravity;

  player.y += yspeed;

  if (player.y + player.height / 2 > stage.stageHeight) {

  restart();

  }

  if (player.y - player.height / 2 < 0) {

  player.y = player.height / 2;

  }

  for (var i: int = 0; i < obstacles.length; ++i) {

  updateObstacle(i);

  }

  scoretxt.text = String(score);

}

function updateObstacle(i: int) {

  var ob: Obstacle = obstacles;

  if (ob == null)

  return;

  ob.x -= ob_speed;

  if (ob.x < -ob.width) {

  changeObstacle(ob);

  }

  if (ob.hitTestPoint(player.x + player.width / 2, player.y + player.height / 2, true) || ob.hitTestPoint(player.x + player.width / 2, player.y - player.height / 2, true) || ob.hitTestPoint(player.x - player.width / 2, player.y + player.height / 2, true) || ob.hitTestPoint(player.x - player.width / 2, player.y - player.height / 2, true)) {

  restart();

  }

  if ((player.x - player.width / 2 > ob.x + ob.width / 2) && !ob.covered) {

  ++score;

  ob.covered = true;

  }

}

function changeObstacle(ob: Obstacle) {

  ob.x = lastob.x + dist_btw_obstacles;

  ob.y = 100 + Math.random() * (stage.stageHeight - 200);

  lastob = ob;

  ob.covered = false;

}

function createObstacle() {

  var ob: Obstacle = new Obstacle();

  if (lastob.x == 0)

  ob.x = 800;

  else

  ob.x = lastob.x + dist_btw_obstacles;

  ob.y = 100 + Math.random() * (stage.stageHeight - 200);

  addChild(ob);

  obstacles.push(ob);

  lastob = ob;

}

TOPICS
ActionScript

Views

202

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

I am a beginner in the Actionscript language. How do I resolve Syntax error: package is unexpected?

New Here ,
Jun 29, 2019

Copy link to clipboard

Copied

When I click CTRL + ENTER there is always the error: Syntax error: package is unexpected. How do I fix this error ??

I leave my code if anyone wants to help, thanks!

stop();

package {

import flash.display.MovieClip;

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

import flash.events.Event;

public class Main extends MovieClip {

const gravity: Number = 1.5;

const dist_btw_obstacles: Number = 300;

const ob_speed: Number = 8;

const jump_force: Number = 15;

var player: Player = new Player();

var lastob: Obstacle = new Obstacle();

var obstacles: Array = new Array();

var yspeed: Number = 0;

var score: Number = 0;

public function Main() {

init();

}

function init(): void {

player = new Player();

lastob = new Obstacle();

obstacles = new Array();

yspeed = 0;

score = 0;

player.x = stage.stageWidth / 2;

player.y = stage.stageHeight / 2;

addChild(player);

createObstacle();

createObstacle();

createObstacle();

addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);

stage.addEventListener(KeyboardEvent.KEY_UP, key_up);

}

private function key_up(event: KeyboardEvent) {

if (event.keyCode == Keyboard.SPACE) {

yspeed = -jump_force;

}

}

function restart() {

if (contains(player))

removeChild(player);

for (var i: int = 0; i < obstacles.length; ++i) {

if (contains(obstacles) && obstacles != null)

removeChild(obstacles);

obstacles = null;

}

obstacles.slice(0);

init();

}

function onEnterFrameHandler(event: Event) {

//update player

yspeed += gravity;

player.y += yspeed;

if (player.y + player.height / 2 > stage.stageHeight) {

restart();

}

if (player.y - player.height / 2 < 0) {

player.y = player.height / 2;

}

for (var i: int = 0; i < obstacles.length; ++i) {

updateObstacle(i);

}

scoretxt.text = String(score);

}

function updateObstacle(i: int) {

var ob: Obstacle = obstacles;

if (ob == null)

return;

ob.x -= ob_speed;

if (ob.x < -ob.width) {

changeObstacle(ob);

}

if (ob.hitTestPoint(player.x + player.width / 2, player.y + player.height / 2, true) || ob.hitTestPoint(player.x + player.width / 2, player.y - player.height / 2, true) || ob.hitTestPoint(player.x - player.width / 2, player.y + player.height / 2, true) || ob.hitTestPoint(player.x - player.width / 2, player.y - player.height / 2, true)) {

restart();

}

if ((player.x - player.width / 2 > ob.x + ob.width / 2) && !ob.covered) {

++score;

ob.covered = true;

}

}

function changeObstacle(ob: Obstacle) {

ob.x = lastob.x + dist_btw_obstacles;

ob.y = 100 + Math.random() * (stage.stageHeight - 200);

lastob = ob;

ob.covered = false;

}

function createObstacle() {

var ob: Obstacle = new Obstacle();

if (lastob.x == 0)

ob.x = 800;

else

ob.x = lastob.x + dist_btw_obstacles;

ob.y = 100 + Math.random() * (stage.stageHeight - 200);

addChild(ob);

obstacles.push(ob);

lastob = ob;

}

}

}

ActionScript 3

Adobe Community Professional
Correct answer by Colin Holgate | Adobe Community Professional

Anything that can be done with classes can be done in the timeline. Mainly you need to remove anything that talks about package or private or public. The example code also uses other classes, which could be movieclips in the library. Inside those movieclips there could also be timeline code, though in your flappy birds test I don't think there is.

Here is how the timeline code would look, and it does work if you have a Player movieclip and Obstacle movieclip in the library, and a scoretxt dynamic textfield on the stage. In general, following tutorials that have a download of the files would help.

init();

stop();

import flash.display.MovieClip;

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

import flash.events.Event;

const gravity: Number = 1.5;

const dist_btw_obstacles: Number = 300;

const ob_speed: Number = 8;

const jump_force: Number = 15;

var player: Player = new Player();

var lastob: Obstacle = new Obstacle();

var obstacles: Array = new Array();

var yspeed: Number = 0;

var score: Number = 0;

function init(): void {

  player = new Player();

  lastob = new Obstacle();

  obstacles = new Array();

  yspeed = 0;

  score = 0;

  player.x = stage.stageWidth / 2;

  player.y = stage.stageHeight / 2;

  addChild(player);

  createObstacle();

  createObstacle();

  createObstacle();

  addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);

  stage.addEventListener(KeyboardEvent.KEY_UP, key_up);

}

function key_up(event: KeyboardEvent) {

  if (event.keyCode == Keyboard.SPACE) {

  yspeed = -jump_force;

  }

}

function restart() {

  if (contains(player))

  removeChild(player);

  for (var i: int = 0; i < obstacles.length; ++i) {

  if (contains(obstacles) && obstacles != null)

  removeChild(obstacles);

  obstacles = null;

  }

  obstacles.slice(0);

  init();

}

function onEnterFrameHandler(event: Event) {

  //update player

  yspeed += gravity;

  player.y += yspeed;

  if (player.y + player.height / 2 > stage.stageHeight) {

  restart();

  }

  if (player.y - player.height / 2 < 0) {

  player.y = player.height / 2;

  }

  for (var i: int = 0; i < obstacles.length; ++i) {

  updateObstacle(i);

  }

  scoretxt.text = String(score);

}

function updateObstacle(i: int) {

  var ob: Obstacle = obstacles;

  if (ob == null)

  return;

  ob.x -= ob_speed;

  if (ob.x < -ob.width) {

  changeObstacle(ob);

  }

  if (ob.hitTestPoint(player.x + player.width / 2, player.y + player.height / 2, true) || ob.hitTestPoint(player.x + player.width / 2, player.y - player.height / 2, true) || ob.hitTestPoint(player.x - player.width / 2, player.y + player.height / 2, true) || ob.hitTestPoint(player.x - player.width / 2, player.y - player.height / 2, true)) {

  restart();

  }

  if ((player.x - player.width / 2 > ob.x + ob.width / 2) && !ob.covered) {

  ++score;

  ob.covered = true;

  }

}

function changeObstacle(ob: Obstacle) {

  ob.x = lastob.x + dist_btw_obstacles;

  ob.y = 100 + Math.random() * (stage.stageHeight - 200);

  lastob = ob;

  ob.covered = false;

}

function createObstacle() {

  var ob: Obstacle = new Obstacle();

  if (lastob.x == 0)

  ob.x = 800;

  else

  ob.x = lastob.x + dist_btw_obstacles;

  ob.y = 100 + Math.random() * (stage.stageHeight - 200);

  addChild(ob);

  obstacles.push(ob);

  lastob = ob;

}

TOPICS
ActionScript

Views

203

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jun 29, 2019 0
Adobe Community Professional ,
Jun 30, 2019

Copy link to clipboard

Copied

that code needs to be in an as3 (not as2) class file, not on a timeline.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 30, 2019 0
New Here ,
Jun 30, 2019

Copy link to clipboard

Copied

How do I put it in a code where it can stay on the timeline?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 30, 2019 0
Adobe Community Professional ,
Jun 30, 2019

Copy link to clipboard

Copied

Anything that can be done with classes can be done in the timeline. Mainly you need to remove anything that talks about package or private or public. The example code also uses other classes, which could be movieclips in the library. Inside those movieclips there could also be timeline code, though in your flappy birds test I don't think there is.

Here is how the timeline code would look, and it does work if you have a Player movieclip and Obstacle movieclip in the library, and a scoretxt dynamic textfield on the stage. In general, following tutorials that have a download of the files would help.

init();

stop();

import flash.display.MovieClip;

import flash.events.KeyboardEvent;

import flash.ui.Keyboard;

import flash.events.Event;

const gravity: Number = 1.5;

const dist_btw_obstacles: Number = 300;

const ob_speed: Number = 8;

const jump_force: Number = 15;

var player: Player = new Player();

var lastob: Obstacle = new Obstacle();

var obstacles: Array = new Array();

var yspeed: Number = 0;

var score: Number = 0;

function init(): void {

  player = new Player();

  lastob = new Obstacle();

  obstacles = new Array();

  yspeed = 0;

  score = 0;

  player.x = stage.stageWidth / 2;

  player.y = stage.stageHeight / 2;

  addChild(player);

  createObstacle();

  createObstacle();

  createObstacle();

  addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);

  stage.addEventListener(KeyboardEvent.KEY_UP, key_up);

}

function key_up(event: KeyboardEvent) {

  if (event.keyCode == Keyboard.SPACE) {

  yspeed = -jump_force;

  }

}

function restart() {

  if (contains(player))

  removeChild(player);

  for (var i: int = 0; i < obstacles.length; ++i) {

  if (contains(obstacles) && obstacles != null)

  removeChild(obstacles);

  obstacles = null;

  }

  obstacles.slice(0);

  init();

}

function onEnterFrameHandler(event: Event) {

  //update player

  yspeed += gravity;

  player.y += yspeed;

  if (player.y + player.height / 2 > stage.stageHeight) {

  restart();

  }

  if (player.y - player.height / 2 < 0) {

  player.y = player.height / 2;

  }

  for (var i: int = 0; i < obstacles.length; ++i) {

  updateObstacle(i);

  }

  scoretxt.text = String(score);

}

function updateObstacle(i: int) {

  var ob: Obstacle = obstacles;

  if (ob == null)

  return;

  ob.x -= ob_speed;

  if (ob.x < -ob.width) {

  changeObstacle(ob);

  }

  if (ob.hitTestPoint(player.x + player.width / 2, player.y + player.height / 2, true) || ob.hitTestPoint(player.x + player.width / 2, player.y - player.height / 2, true) || ob.hitTestPoint(player.x - player.width / 2, player.y + player.height / 2, true) || ob.hitTestPoint(player.x - player.width / 2, player.y - player.height / 2, true)) {

  restart();

  }

  if ((player.x - player.width / 2 > ob.x + ob.width / 2) && !ob.covered) {

  ++score;

  ob.covered = true;

  }

}

function changeObstacle(ob: Obstacle) {

  ob.x = lastob.x + dist_btw_obstacles;

  ob.y = 100 + Math.random() * (stage.stageHeight - 200);

  lastob = ob;

  ob.covered = false;

}

function createObstacle() {

  var ob: Obstacle = new Obstacle();

  if (lastob.x == 0)

  ob.x = 800;

  else

  ob.x = lastob.x + dist_btw_obstacles;

  ob.y = 100 + Math.random() * (stage.stageHeight - 200);

  addChild(ob);

  obstacles.push(ob);

  lastob = ob;

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 30, 2019 1
New Here ,
Jun 30, 2019

Copy link to clipboard

Copied

Thank you I've been watching and now seeing your code and mine have been able to see the differences between them. Thank you so.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jun 30, 2019 0