{"id":1287,"date":"2011-01-04T16:22:25","date_gmt":"2011-01-04T16:22:25","guid":{"rendered":"http:\/\/www.benoitfreslon.com\/?p=1287"},"modified":"2024-07-19T09:34:18","modified_gmt":"2024-07-19T07:34:18","slug":"actionscript-move-an-object-with-keyboard-with-camera-relative-control","status":"publish","type":"post","link":"https:\/\/benoitfreslon.com\/fr\/news\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\/","title":{"rendered":"ActionScript: Move an object with keyboard with camera relative control"},"content":{"rendered":"<p>Dans ce tutoriel je vais vous montrer comment d\u00e9placer un objet avec le clavier suivant la position de la cam\u00e9ra.<\/p>\n<p><strong>Contr\u00f4les relatifs \u00e0 la cam\u00e9ra ?<\/strong><\/p>\n<p>La direction de votre objet est calcul\u00e9e en fonction de la position de la cam\u00e9ra.(Mario 64, Uncharted, GTA 3, etc.).<\/p>\n<p>Exemple : Si j&rsquo;appuis sur le bouton droit le personnage va aller vers la droite de mon \u00e9cran.<\/p>\n<p><strong>Comment faire un contr\u00f4le relatif ?<\/strong><\/p>\n<ul>\n<li>R\u00e9cup\u00e9rer les informations du clavier<\/li>\n<li>Cr\u00e9er un vecteur de direction<\/li>\n<li>D\u00e9placer l&rsquo;objet en fonction du vecteur entre chaque frame<\/li>\n<\/ul>\n<pre class=\"brush:as3\">\/\/ Assigner 4 bool\u00e9ens pour chaque touche\r\nvar keyUp = false;\r\nvar keyDown = false;\r\nvar keyLeft = false;\r\nvar keyRight = false;\r\n\/\/ Ajouter une \u00e9couteur d'\u00e9v\u00e9nement clavier pour une touche press\u00e9e sur la sc\u00e8ne\r\nstage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);\r\nfunction pressKey(pEvent) {\r\n\t\/\/ Si une touche est enfonc\u00e9e, changer la valeur du boolean (true) assign\u00e9e pour la touche correspondante\r\n\tif (pEvent.keyCode == 38) {\r\n\t\tkeyUp = true;\r\n\t} else if (pEvent.keyCode == 40) {\r\n\t\tkeyDown = true;\r\n\t} else if (pEvent.keyCode == 37) {\r\n\t\tkeyLeft = true;\r\n\t} else if (pEvent.keyCode == 39) {\r\n\t\tkeyRight = true;\r\n\t}\r\n}\r\n\/\/ Assigner 4 bool\u00e9ens pour chaque touche\r\nvar keyUp = false;\r\nvar keyDown = false;\r\nvar keyLeft = false;\r\nvar keyRight = false;\r\n\r\n\/\/ Ajouter une \u00e9couteur d'\u00e9v\u00e9nement clavier pour une touche press\u00e9e sur la sc\u00e8ne\r\nstage.addEventListener(KeyboardEvent.KEY_DOWN, pressKey);\r\n\r\nfunction pressKey(pEvent) {\r\n\t\/\/ Si une touche est enfonc\u00e9e, changer la valeur du boolean (true) assign\u00e9e pour la touche correspondante\r\n\tif (pEvent.keyCode == 38) {\r\n\t\tkeyUp = true;\r\n\t} else if (pEvent.keyCode == 40) {\r\n\t\tkeyDown = true;\r\n\t} else if (pEvent.keyCode == 37) {\r\n\t\tkeyLeft = true;\r\n\t} else if (pEvent.keyCode == 39) {\r\n\t\tkeyRight = true;\r\n\t}\r\n}\r\n\r\n\/\/ Assigner la vitesse de l'objet\r\nvar speed = 6;\r\n\r\n\/\/ Ajouter un \u00e9couteur d'\u00e9v\u00e9nement entre chaque frame sur l'objet\r\nmyCircle.addEventListener(Event.ENTER_FRAME, circleEnterFrame);\r\n\r\nfunction circleEnterFrame(pEvent) {\r\n\t\/\/ Cr\u00e9er et initialiser un vecteur 2D\r\n\tvar vector = new Point(0,0);\r\n\tif (keyUp) {\r\n\t\t\/\/Si la touche Haut est enfonc\u00e9e assigner une nouvelle valeur au vecteur sur y\r\n\t\tvector.y +=  -1;\r\n\t}\r\n\tif (keyDown) {\r\n\t\t\/\/ Si la touche Bas est enfonc\u00e9e assigner une nouvelle valeur au vecteur sur y\r\n\t\tvector.y +=  1;\r\n\t}\r\n\tif (keyLeft) {\r\n\t\t\/\/ Si la touche Gauche est enfonc\u00e9e assigner une nouvelle valeur au vecteur sur x\r\n\t\tvector.x +=  -1;\r\n\t}\r\n\tif (keyRight) {\r\n\t\t\/\/ Si la touche Right est enfonc\u00e9e assigner une nouvelle valeur au vecteur sur x\r\n\t\tvector.x +=  1;\r\n\t}\r\n\t\/\/ Calculer l'angle en radian form\u00e9 par le vecteur de vitesse\r\n\tvar angle = Math.atan2(vector.y,vector.x);\r\n\t\/\/ Si la taille du vecteur n'est pas nul\r\n\tif (vector.length &gt; 0) {\r\n\t\t\/\/ D\u00e9placer l'objet en fonction de l'angle form\u00e9 et de la vitesse\r\n\t\tpEvent.currentTarget.x +=  Math.cos(angle) * speed;\r\n\t\tpEvent.currentTarget.y +=  Math.sin(angle) * speed;\r\n\t}\r\n}<\/pre>\n<p>[swf:\/wp-content\/uploads\/2011\/01\/www.benoitfreslon.com-Move-an-objet-with-keyboard-with-camera-relative-control.swf 550 400]<br \/>\nT\u00e9l\u00e9charger la source : www.benoitfreslon.com-Move-an-objet-with-keyboard-with-camera-relative-control.zip<\/p>","protected":false},"excerpt":{"rendered":"<p>Dans ce tutoriel je vais vous montrer comment d\u00e9placer un objet avec le clavier suivant la position de la cam\u00e9ra&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1],"tags":[109,105,409,302,108,102,106],"class_list":["post-1287","post","type-post","status-publish","format-standard","hentry","category-news","tag-arrows","tag-camera","tag-clavier","tag-actionscript","tag-key","tag-move","tag-relative","wpcat-1-id"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>ActionScript: Move an object with keyboard with camera relative control - Beno\u00eet Freslon<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/benoitfreslon.com\/fr\/news\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\/\" \/>\n<meta name=\"twitter:label1\" content=\"\u00c9crit par\" \/>\n\t<meta name=\"twitter:data1\" content=\"Benoit Freslon\" \/>\n\t<meta name=\"twitter:label2\" content=\"Dur\u00e9e de lecture estim\u00e9e\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/benoitfreslon.com\\\/news\\\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/benoitfreslon.com\\\/news\\\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\\\/\"},\"author\":{\"name\":\"Benoit Freslon\",\"@id\":\"https:\\\/\\\/benoitfreslon.com\\\/#\\\/schema\\\/person\\\/fe9fe28a6b6c2b56648448b977fd6b78\"},\"headline\":\"ActionScript: Move an object with keyboard with camera relative control\",\"datePublished\":\"2011-01-04T16:22:25+00:00\",\"dateModified\":\"2024-07-19T07:34:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/benoitfreslon.com\\\/news\\\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\\\/\"},\"wordCount\":223,\"keywords\":[\"arrows\",\"camera\",\"clavier\",\"Flash development\",\"key\",\"move\",\"relative\"],\"articleSection\":[\"Actualit\u00e9s\"],\"inLanguage\":\"fr-FR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/benoitfreslon.com\\\/fr\\\/news\\\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\\\/\",\"url\":\"https:\\\/\\\/benoitfreslon.com\\\/fr\\\/news\\\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\\\/\",\"name\":\"ActionScript: Move an object with keyboard with camera relative control - Beno\u00eet Freslon\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/benoitfreslon.com\\\/fr\\\/#website\"},\"datePublished\":\"2011-01-04T16:22:25+00:00\",\"dateModified\":\"2024-07-19T07:34:18+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/benoitfreslon.com\\\/#\\\/schema\\\/person\\\/fe9fe28a6b6c2b56648448b977fd6b78\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/benoitfreslon.com\\\/fr\\\/news\\\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\\\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[[\"https:\\\/\\\/benoitfreslon.com\\\/fr\\\/news\\\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\\\/\"]]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/benoitfreslon.com\\\/fr\\\/news\\\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/benoitfreslon.com\\\/fr\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"ActionScript: Move an object with keyboard with camera relative control\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/benoitfreslon.com\\\/fr\\\/#website\",\"url\":\"https:\\\/\\\/benoitfreslon.com\\\/fr\\\/\",\"name\":\"Beno\u00eet Freslon\",\"description\":\"[:en]Game designer | D\u00e9veloppeur de jeux ind\u00e9pendant | Freelance | Enseignant | Blogueur[:fr]Game designer | Independent game developer | Freelance | Teacher | Blogger[:]\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/benoitfreslon.com\\\/fr\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/benoitfreslon.com\\\/#\\\/schema\\\/person\\\/fe9fe28a6b6c2b56648448b977fd6b78\",\"name\":\"Benoit Freslon\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/0b828da2527f91fb7c86dab67050492f4303279afa245d6e718ea1c6e6f00bb2?s=96&d=identicon&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/0b828da2527f91fb7c86dab67050492f4303279afa245d6e718ea1c6e6f00bb2?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/0b828da2527f91fb7c86dab67050492f4303279afa245d6e718ea1c6e6f00bb2?s=96&d=identicon&r=g\",\"caption\":\"Benoit Freslon\"},\"sameAs\":[\"https:\\\/\\\/benoitfreslon.com\",\"https:\\\/\\\/www.instagram.com\\\/benoitfreslon\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/benoitfreslon\",\"https:\\\/\\\/x.com\\\/BenoitFreslon\",\"https:\\\/\\\/www.youtube.com\\\/benoitfreslon\"],\"url\":\"https:\\\/\\\/benoitfreslon.com\\\/fr\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"ActionScript: Move an object with keyboard with camera relative control - Beno\u00eet Freslon","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/benoitfreslon.com\/fr\/news\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\/","twitter_misc":{"\u00c9crit par":"Benoit Freslon","Dur\u00e9e de lecture estim\u00e9e":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/benoitfreslon.com\/news\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\/#article","isPartOf":{"@id":"https:\/\/benoitfreslon.com\/news\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\/"},"author":{"name":"Benoit Freslon","@id":"https:\/\/benoitfreslon.com\/#\/schema\/person\/fe9fe28a6b6c2b56648448b977fd6b78"},"headline":"ActionScript: Move an object with keyboard with camera relative control","datePublished":"2011-01-04T16:22:25+00:00","dateModified":"2024-07-19T07:34:18+00:00","mainEntityOfPage":{"@id":"https:\/\/benoitfreslon.com\/news\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\/"},"wordCount":223,"keywords":["arrows","camera","clavier","Flash development","key","move","relative"],"articleSection":["Actualit\u00e9s"],"inLanguage":"fr-FR"},{"@type":"WebPage","@id":"https:\/\/benoitfreslon.com\/fr\/news\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\/","url":"https:\/\/benoitfreslon.com\/fr\/news\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\/","name":"ActionScript: Move an object with keyboard with camera relative control - Beno\u00eet Freslon","isPartOf":{"@id":"https:\/\/benoitfreslon.com\/fr\/#website"},"datePublished":"2011-01-04T16:22:25+00:00","dateModified":"2024-07-19T07:34:18+00:00","author":{"@id":"https:\/\/benoitfreslon.com\/#\/schema\/person\/fe9fe28a6b6c2b56648448b977fd6b78"},"breadcrumb":{"@id":"https:\/\/benoitfreslon.com\/fr\/news\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":[["https:\/\/benoitfreslon.com\/fr\/news\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\/"]]}]},{"@type":"BreadcrumbList","@id":"https:\/\/benoitfreslon.com\/fr\/news\/actionscript-move-an-object-with-keyboard-with-camera-relative-control\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/benoitfreslon.com\/fr\/"},{"@type":"ListItem","position":2,"name":"ActionScript: Move an object with keyboard with camera relative control"}]},{"@type":"WebSite","@id":"https:\/\/benoitfreslon.com\/fr\/#website","url":"https:\/\/benoitfreslon.com\/fr\/","name":"Beno\u00eet Freslon","description":"[:en]Game designer | D\u00e9veloppeur de jeux ind\u00e9pendant | Freelance | Enseignant | Blogueur[:fr]Game designer | Independent game developer | Freelance | Teacher | Blogger[:]","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/benoitfreslon.com\/fr\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"},{"@type":"Person","@id":"https:\/\/benoitfreslon.com\/#\/schema\/person\/fe9fe28a6b6c2b56648448b977fd6b78","name":"Benoit Freslon","image":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/secure.gravatar.com\/avatar\/0b828da2527f91fb7c86dab67050492f4303279afa245d6e718ea1c6e6f00bb2?s=96&d=identicon&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/0b828da2527f91fb7c86dab67050492f4303279afa245d6e718ea1c6e6f00bb2?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/0b828da2527f91fb7c86dab67050492f4303279afa245d6e718ea1c6e6f00bb2?s=96&d=identicon&r=g","caption":"Benoit Freslon"},"sameAs":["https:\/\/benoitfreslon.com","https:\/\/www.instagram.com\/benoitfreslon","https:\/\/www.linkedin.com\/in\/benoitfreslon","https:\/\/x.com\/BenoitFreslon","https:\/\/www.youtube.com\/benoitfreslon"],"url":"https:\/\/benoitfreslon.com\/fr\/author\/admin\/"}]}},"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/pLAe3-kL","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/benoitfreslon.com\/fr\/wp-json\/wp\/v2\/posts\/1287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/benoitfreslon.com\/fr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benoitfreslon.com\/fr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benoitfreslon.com\/fr\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benoitfreslon.com\/fr\/wp-json\/wp\/v2\/comments?post=1287"}],"version-history":[{"count":41,"href":"https:\/\/benoitfreslon.com\/fr\/wp-json\/wp\/v2\/posts\/1287\/revisions"}],"predecessor-version":[{"id":39127,"href":"https:\/\/benoitfreslon.com\/fr\/wp-json\/wp\/v2\/posts\/1287\/revisions\/39127"}],"wp:attachment":[{"href":"https:\/\/benoitfreslon.com\/fr\/wp-json\/wp\/v2\/media?parent=1287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benoitfreslon.com\/fr\/wp-json\/wp\/v2\/categories?post=1287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benoitfreslon.com\/fr\/wp-json\/wp\/v2\/tags?post=1287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}